org.ehcache.config.builders.UserManagedCacheBuilder Java Examples

The following examples show how to use org.ehcache.config.builders.UserManagedCacheBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserManagedCacheEvictionTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_eviction() throws Exception {
  UserManagedCache<Number, String> cache = UserManagedCacheBuilder.newUserManagedCacheBuilder(Number.class, String.class)
      .withResourcePools(newResourcePoolsBuilder().heap(1, EntryUnit.ENTRIES))
      .build(true);
  assertThat(cache.getRuntimeConfiguration().getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(),
      equalTo(1L));

  // we put 3 elements, but there's only capacity for 1
  for (int i = 0; i < 3; i++) {
    cache.putIfAbsent(i, "" + i);
  }

  // we must find at most 1 non empty value
  int nullValuesFound = 0;
  for (int i = 0; i < 3; i++) {
    String retrievedValue = cache.get(i);
    if (retrievedValue == null) {
      nullValuesFound ++;
    }
  }
  assertThat("The capacity of the store is 1, and we found more than 1 non empty value in it !", nullValuesFound, is(2));
}
 
Example #2
Source File: UserManagedCaches.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void userManagedDiskCache() throws Exception {
  // tag::persistentUserManagedCache[]
  LocalPersistenceService persistenceService = new DefaultLocalPersistenceService(new DefaultPersistenceConfiguration(new File(getStoragePath(), "myUserData"))); // <1>

  PersistentUserManagedCache<Long, String> cache = UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, String.class)
      .with(new UserManagedPersistenceContext<>("cache-name", persistenceService)) // <2>
      .withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
          .heap(10L, EntryUnit.ENTRIES)
          .disk(10L, MemoryUnit.MB, true)) // <3>
      .build(true);

  // Work with the cache
  cache.put(42L, "The Answer!");
  assertThat(cache.get(42L), is("The Answer!"));

  cache.close(); // <4>
  cache.destroy(); // <5>

  persistenceService.stop(); // <6>
  // end::persistentUserManagedCache[]
}
 
Example #3
Source File: UserManagedCaches.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void userManagedListenerCache() throws Exception {
  // tag::userManagedListenerCache[]

  UserManagedCache<Long, String> cache = UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, String.class)
      .withEventExecutors(Executors.newSingleThreadExecutor(), Executors.newFixedThreadPool(5)) // <1>
      .withEventListeners(CacheEventListenerConfigurationBuilder
          .newEventListenerConfiguration(ListenerObject.class, EventType.CREATED, EventType.UPDATED)
          .asynchronous()
          .unordered()) // <2>
      .withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
          .heap(3, EntryUnit.ENTRIES))
      .build(true);

  cache.put(1L, "Put it");
  cache.put(1L, "Update it");

  cache.close();
  // end::userManagedListenerCache[]
}
 
Example #4
Source File: TestUserManagedCache.java    From jframe with Apache License 2.0 6 votes vote down vote up
public void persistenceServiceTest() {
    LocalPersistenceService persistenceService = new DefaultLocalPersistenceService(
            new DefaultPersistenceConfiguration(new File("", "myUserData")));

    PersistentUserManagedCache<Long, String> cache = UserManagedCacheBuilder
            .newUserManagedCacheBuilder(Long.class, String.class)
            .with(new UserManagedPersistenceContext<Long, String>("cache-name", persistenceService))
            .withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10L, EntryUnit.ENTRIES).disk(10L,
                    MemoryUnit.MB, true))
            .build(true);

    // Work with the cache
    cache.put(42L, "The Answer!");
    // assertThat(cache.get(42L), is("The Answer!"));

    cache.close();
    try {
        cache.destroy();
    } catch (CachePersistenceException e) {
        e.printStackTrace();
    }

    persistenceService.stop();
}
 
Example #5
Source File: UserManagedCacheLoaderWriterTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testLoaderWriterWithUserManagedCache() throws Exception {
  CacheLoaderWriter<Long, Long> cacheLoaderWriter = mock(CacheLoaderWriter.class);

  UserManagedCache<Long, Long> userManagedCache = UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, Long.class)
          .withResourcePools(newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES))
          .withLoaderWriter(cacheLoaderWriter).build(true);

  userManagedCache.put(1L, 1L);
  verify(cacheLoaderWriter, times(1)).write(eq(1L), eq(1L));

  when(cacheLoaderWriter.load(anyLong())).thenReturn(2L);
  Assert.assertThat(userManagedCache.get(2L), Matchers.is(2L));
}
 
Example #6
Source File: UserManagedCaches.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void userManagedCacheExample() {
  // tag::userManagedCacheExample[]
  UserManagedCache<Long, String> userManagedCache =
      UserManagedCacheBuilder.newUserManagedCacheBuilder(Long.class, String.class)
          .build(false); // <1>
  userManagedCache.init(); // <2>

  userManagedCache.put(1L, "da one!"); // <3>

  userManagedCache.close(); // <4>
  // end::userManagedCacheExample[]
}
 
Example #7
Source File: TestUserManagedCache.java    From jframe with Apache License 2.0 5 votes vote down vote up
public void init() {
    UserManagedCache<Long, String> userManagedCache = UserManagedCacheBuilder
            .newUserManagedCacheBuilder(Long.class, String.class).build(false);
    userManagedCache.init();

    userManagedCache.put(1L, "da one!");

    userManagedCache.close();
}
 
Example #8
Source File: TestEhcache.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void userManagedCacheTest() {
    UserManagedCache<Long, String> userManagedCache = UserManagedCacheBuilder
            .newUserManagedCacheBuilder(Long.class, String.class).build(false);
    userManagedCache.init();

    userManagedCache.put(1L, "da one!");
    userManagedCache.close();

    String value = userManagedCache.get(1L);
    LOG.info(value);
}