org.ehcache.UserManagedCache Java Examples

The following examples show how to use org.ehcache.UserManagedCache. 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 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 #3
Source File: DataCache.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void setupCache() {
    cache = (UserManagedCache<String, List<String>>) (UserManagedCache<?, ?>) newUserManagedCacheBuilder(String.class, List.class)
      .identifier("data-cache")
      .build(true);
    logger.info("Cache setup is done");
}
 
Example #4
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 #5
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 #6
Source File: UserManagedCacheBuilderTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypedCacheWithExpirationPolicy() {
  try (UserManagedCache<String, String> cache = UserManagedCacheBuilder.newUserManagedCacheBuilder(String.class, String.class)
    .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(30)))
    .build(true)) {
    assertThat(cache, notNullValue());
  }
}
 
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);
}
 
Example #9
Source File: UserManagedCacheBuilder.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
T cast(UserManagedCache<K, V> cache) {
  return (T)cache;
}
 
Example #10
Source File: UserManagedCacheBuilder.java    From ehcache3 with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@code UserManagedCacheBuilder}.
 *
 * @param keyType the cache key type
 * @param valueType the cache value type
 * @param <K> the key type
 * @param <V> the value type
 * @return the new builder
 */
public static <K, V> UserManagedCacheBuilder<K, V, UserManagedCache<K, V>> newUserManagedCacheBuilder(Class<K> keyType, Class<V> valueType) {
  return new UserManagedCacheBuilder<>(keyType, valueType);
}