org.ehcache.config.CacheRuntimeConfiguration Java Examples

The following examples show how to use org.ehcache.config.CacheRuntimeConfiguration. 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: EhcacheActionProviderTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCallAction_happyPathNoParam() throws Exception {
  EhcacheActionProvider ehcacheActionProvider = new EhcacheActionProvider(cmConfig_0);

  Ehcache<Object, Object> ehcache = mock(Ehcache.class);
  CacheRuntimeConfiguration<Object, Object> cacheRuntimeConfiguration = mock(CacheRuntimeConfiguration.class);
  when(cacheRuntimeConfiguration.getClassLoader()).thenReturn(ClassLoader.getSystemClassLoader());
  when(ehcache.getRuntimeConfiguration()).thenReturn(cacheRuntimeConfiguration);
  ehcacheActionProvider.register(new CacheBinding("cache-0", ehcache));

  Context context = cmContext_0.with("cacheName", "cache-0");

  ehcacheActionProvider.callAction(context, "clear", Void.class);

  verify(ehcache, times(1)).clear();
}
 
Example #2
Source File: EhcacheActionProviderTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCallAction_happyPathWithParams() throws Exception {
  EhcacheActionProvider ehcacheActionProvider = new EhcacheActionProvider(cmConfig_0);

  @SuppressWarnings("unchecked")
  Ehcache<Long, String> ehcache = mock(Ehcache.class);
  @SuppressWarnings("unchecked")
  CacheRuntimeConfiguration<Long, String> cacheRuntimeConfiguration = mock(CacheRuntimeConfiguration.class);
  when(cacheRuntimeConfiguration.getClassLoader()).thenReturn(ClassLoader.getSystemClassLoader());
  when(cacheRuntimeConfiguration.getKeyType()).thenReturn(Long.class);
  when(ehcache.getRuntimeConfiguration()).thenReturn(cacheRuntimeConfiguration);
  ehcacheActionProvider.register(new CacheBinding("cache-0", ehcache));


  Context context = cmContext_0.with("cacheName", "cache-0");

  ehcacheActionProvider.callAction(context, "get", Object.class, new Parameter("1", Object.class.getName()));

  verify(ehcache, times(1)).get(eq(1L));
}
 
Example #3
Source File: EhcacheActionProviderTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCallAction_noSuchMethodName() throws Exception {
  EhcacheActionProvider ehcacheActionProvider = new EhcacheActionProvider(cmConfig_0);

  Ehcache<Object, Object> ehcache = mock(Ehcache.class);
  CacheRuntimeConfiguration<Object, Object> cacheRuntimeConfiguration = mock(CacheRuntimeConfiguration.class);
  when(cacheRuntimeConfiguration.getClassLoader()).thenReturn(ClassLoader.getSystemClassLoader());
  when(ehcache.getRuntimeConfiguration()).thenReturn(cacheRuntimeConfiguration);
  ehcacheActionProvider.register(new CacheBinding("cache-0", ehcache));

  Context context = cmContext_0.with("cacheName", "cache-0");

  try {
    ehcacheActionProvider.callAction(context, "clearer", Void.class);
    fail("expected IllegalArgumentException");
  } catch (IllegalArgumentException iae) {
    // expected
  }
}
 
Example #4
Source File: EhcacheActionProviderTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCallAction_noSuchMethod() throws Exception {
  EhcacheActionProvider ehcacheActionProvider = new EhcacheActionProvider(cmConfig_0);

  @SuppressWarnings("unchecked")
  Ehcache<Long, String> ehcache = mock(Ehcache.class);
  @SuppressWarnings("unchecked")
  CacheRuntimeConfiguration<Long, String> cacheRuntimeConfiguration = mock(CacheRuntimeConfiguration.class);
  when(cacheRuntimeConfiguration.getClassLoader()).thenReturn(ClassLoader.getSystemClassLoader());
  when(ehcache.getRuntimeConfiguration()).thenReturn(cacheRuntimeConfiguration);
  ehcacheActionProvider.register(new CacheBinding("cache-0", ehcache));

  Context context = Context.empty()
      .with("cacheManagerName", "cache-manager-1")
      .with("cacheName", "cache-0");

  try {
    ehcacheActionProvider.callAction(context, "get", Object.class, new Parameter(0L, Long.class.getName()));
    fail("expected IllegalArgumentException");
  } catch (IllegalArgumentException iae) {
    // expected
  }

  verify(ehcache, times(0)).get(null);
}
 
Example #5
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testUsingEhcacheConfiguration() throws Exception {
  // tag::ehcacheBasedConfigurationExample[]
  CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.heap(10)).build(); // <1>

  Cache<Long, String> cache = cacheManager.createCache("myCache",
      Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration)); // <2>

  Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class);
  configuration.unwrap(CacheConfiguration.class); // <3>

  configuration.unwrap(CacheRuntimeConfiguration.class); // <4>

  try {
    cache.getConfiguration(CompleteConfiguration.class); // <5>
    throw new AssertionError("IllegalArgumentException expected");
  } catch (IllegalArgumentException iaex) {
    // Expected
  }
  // end::ehcacheBasedConfigurationExample[]
}
 
Example #6
Source File: TestJCache.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void cacheConfigTest() {
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();

    MutableConfiguration<Long, String> configuration = new MutableConfiguration<Long, String>();
    configuration.setTypes(Long.class, String.class);
    Cache<Long, String> cache = cacheManager.createCache("someCache", configuration);

    CompleteConfiguration<Long, String> completeConfiguration = cache.getConfiguration(CompleteConfiguration.class);

    Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);

    CacheRuntimeConfiguration<Long, String> runtimeConfiguration = eh107Configuration
            .unwrap(CacheRuntimeConfiguration.class);
}
 
Example #7
Source File: TestJCache.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void configEhcache2Jsr107() {
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();

    CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(10)).build();

    Cache<Long, String> cache = cacheManager.createCache("myCache",
            Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));

    Eh107Configuration<Long, String> configuration = cache.getConfiguration(Eh107Configuration.class);
    configuration.unwrap(CacheConfiguration.class);

    configuration.unwrap(CacheRuntimeConfiguration.class);

    try {
        cache.getConfiguration(CompleteConfiguration.class);
        throw new AssertionError("IllegalArgumentException expected");
    } catch (IllegalArgumentException iaex) {
        // Expected
    }
}
 
Example #8
Source File: BaseJCacheTester.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
private <K, V> void inspectCacheConfig(Cache<K, V> myJCache) {
  //get the configuration to print the size on heap
  CacheRuntimeConfiguration<K, V> ehcacheConfig = (CacheRuntimeConfiguration<K, V>) myJCache
    .getConfiguration(Eh107Configuration.class)
    .unwrap(CacheRuntimeConfiguration.class);
  long heapSize = ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize();
  LOGGER.info(ehcacheConfig.toString());
  LOGGER.info("Cache testing - Cache {} with heap capacity = {}", myJCache.getName(), heapSize);
}
 
Example #9
Source File: EhCacheBuilder.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Cache<K, V> build(CacheManager manager) {
  checkNotNull(manager);
  checkNotNull(keyType);
  checkNotNull(valueType);
  checkNotNull(name);
  checkNotNull(expiryFactory);

  CacheConfigurationBuilder<K, V> builder = CacheConfigurationBuilder.newCacheConfigurationBuilder(
      keyType,
      valueType,
      ResourcePoolsBuilder.heap(cacheSize));

  builder.withExpiry(mapToEhCacheExpiry(expiryFactory.create()));

  Cache<K, V> cache = manager.createCache(name,  Eh107Configuration.fromEhcacheCacheConfiguration(builder));

  manager.enableStatistics(name, statisticsEnabled);
  manager.enableManagement(name, managementEnabled);

  if (persister != null) {
    CacheEventListener listener = (final CacheEvent cacheEvent) ->
        persister.accept((K) cacheEvent.getKey(), (V) cacheEvent.getOldValue());

    Eh107Configuration<K, V> configuration = cache.getConfiguration(Eh107Configuration.class);
    configuration.unwrap(CacheRuntimeConfiguration.class)
        .registerCacheEventListener(listener, EventOrdering.UNORDERED, EventFiring.ASYNCHRONOUS,
            EventType.EVICTED, EventType.REMOVED, EventType.EXPIRED);
  }

  return cache;
}
 
Example #10
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testGettingToEhcacheConfiguration() {
  // tag::mutableConfigurationExample[]
  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  Cache<Long, String> cache = cacheManager.createCache("someCache", configuration); // <1>

  CompleteConfiguration<Long, String> completeConfiguration = cache.getConfiguration(CompleteConfiguration.class); // <2>

  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class); // <3>

  CacheRuntimeConfiguration<Long, String> runtimeConfiguration = eh107Configuration.unwrap(CacheRuntimeConfiguration.class); // <4>
  // end::mutableConfigurationExample[]
  assertThat(completeConfiguration, notNullValue());
  assertThat(runtimeConfiguration, notNullValue());

  // Check uses default JSR-107 expiry
  long nanoTime = System.nanoTime();
  LOGGER.info("Seeding random with {}", nanoTime);
  Random random = new Random(nanoTime);
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForCreation(random.nextLong(), Long.toOctalString(random.nextLong())),
              equalTo(org.ehcache.expiry.ExpiryPolicy.INFINITE));
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForAccess(random.nextLong(),
    () -> Long.toOctalString(random.nextLong())), nullValue());
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForUpdate(random.nextLong(),
    () -> Long.toOctalString(random.nextLong()), Long.toOctalString(random.nextLong())), nullValue());
}
 
Example #11
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testWithoutEhcacheExplicitDependencyAndNoCodeChanges() throws Exception {
  CacheManager manager = cachingProvider.getCacheManager(
      getClass().getResource("/org/ehcache/docs/ehcache-jsr107-template-override.xml").toURI(),
      getClass().getClassLoader());

  // tag::jsr107SupplementWithTemplatesExample[]
  MutableConfiguration<Long, Client> mutableConfiguration = new MutableConfiguration<>();
  mutableConfiguration.setTypes(Long.class, Client.class); // <1>

  Cache<Long, Client> anyCache = manager.createCache("anyCache", mutableConfiguration); // <2>

  CacheRuntimeConfiguration<Long, Client> ehcacheConfig = (CacheRuntimeConfiguration<Long, Client>)anyCache.getConfiguration(
      Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class); // <3>
  ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(); // <4>

  Cache<Long, Client> anotherCache = manager.createCache("byRefCache", mutableConfiguration);
  assertFalse(anotherCache.getConfiguration(Configuration.class).isStoreByValue()); // <5>

  MutableConfiguration<String, Client> otherConfiguration = new MutableConfiguration<>();
  otherConfiguration.setTypes(String.class, Client.class);
  otherConfiguration.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // <6>

  Cache<String, Client> foosCache = manager.createCache("foos", otherConfiguration);// <7>
  CacheRuntimeConfiguration<Long, Client> foosEhcacheConfig = (CacheRuntimeConfiguration<Long, Client>)foosCache.getConfiguration(
      Eh107Configuration.class).unwrap(CacheRuntimeConfiguration.class);
  Client client1 = new Client("client1", 1);
  foosEhcacheConfig.getExpiryPolicy().getExpiryForCreation(42L, client1).toMinutes(); // <8>

  CompleteConfiguration<String, String> foosConfig = foosCache.getConfiguration(CompleteConfiguration.class);

  try {
    final Factory<ExpiryPolicy> expiryPolicyFactory = foosConfig.getExpiryPolicyFactory();
    ExpiryPolicy expiryPolicy = expiryPolicyFactory.create(); // <9>
    throw new AssertionError("Expected UnsupportedOperationException");
  } catch (UnsupportedOperationException e) {
    // Expected
  }
  // end::jsr107SupplementWithTemplatesExample[]
  assertThat(ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(), is(20L));
  assertThat(foosEhcacheConfig.getExpiryPolicy().getExpiryForCreation(42L, client1),
      is(java.time.Duration.ofMinutes(2)));
}
 
Example #12
Source File: UserManagedCacheBuilderTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Override
public CacheRuntimeConfiguration<K, V> getRuntimeConfiguration() {
  throw new UnsupportedOperationException("Implement me!");
}
 
Example #13
Source File: PersistentUserManagedEhcache.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CacheRuntimeConfiguration<K, V> getRuntimeConfiguration() {
  return cache.getRuntimeConfiguration();
}
 
Example #14
Source File: EhcacheBase.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CacheRuntimeConfiguration<K, V> getRuntimeConfiguration() {
  return runtimeConfiguration;
}
 
Example #15
Source File: Cache.java    From ehcache3 with Apache License 2.0 2 votes vote down vote up
/**
 * Exposes the {@link org.ehcache.config.CacheRuntimeConfiguration} associated with this Cache instance.
 *
 * @return the configuration currently in use
 */
CacheRuntimeConfiguration<K, V> getRuntimeConfiguration();
 
Example #16
Source File: DefaultConfiguration.java    From ehcache3 with Apache License 2.0 2 votes vote down vote up
/**
 * Replaces a {@link CacheConfiguration} with a {@link CacheRuntimeConfiguration} for the provided alias.
 *
 * @param alias the alias of the cache
 * @param config the existing configuration
 * @param runtimeConfiguration the new configuration
 * @param <K> the key type
 * @param <V> the value type
 *
 * @throws IllegalStateException if the replace fails
 */
public <K, V> void replaceCacheConfiguration(final String alias, final CacheConfiguration<K, V> config, final CacheRuntimeConfiguration<K, V> runtimeConfiguration) {
  if (!caches.replace(alias, config, runtimeConfiguration)) {
    throw new IllegalStateException("The expected configuration doesn't match!");
  }
}