org.ehcache.jsr107.Eh107Configuration Java Examples

The following examples show how to use org.ehcache.jsr107.Eh107Configuration. 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: CacheConfiguration.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    BeanClassLoaderAwareJCacheRegionFactory.setBeanClassLoader(this.getClass().getClassLoader());
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #2
Source File: JCacheCacheFactory.java    From cache2k-benchmark with Apache License 2.0 6 votes vote down vote up
@Override
protected <K, V> BenchmarkCache<K, V> createSpecialized(
  final Class<K> _keyType, final Class<V> _valueType, final int _maxElements) {
  MyBenchmarkCacheAdapter c = new MyBenchmarkCacheAdapter();
  String _cacheName = constructCacheName(_maxElements);
  CacheManager mgr = resolveCacheManager();
  if (mgr.getClass().getName().toString().contains("Eh107")) {
     CacheConfiguration<K, V> _eh107Configuration =
       CacheConfigurationBuilder.newCacheConfigurationBuilder(_keyType, _valueType,
        ResourcePoolsBuilder.heap(_maxElements)).build();
      c.cache = mgr.createCache(_cacheName,
        Eh107Configuration.fromEhcacheCacheConfiguration(_eh107Configuration));
  } else if (mgr.getClass().getName().toString().contains("cache2k")) {
    c.cache = mgr.createCache(_cacheName,
      ExtendedMutableConfiguration.of(
        Cache2kBuilder.of(_keyType, _valueType)
          .entryCapacity(_maxElements)));
  } else {
    c.cache = mgr.getCache(_cacheName);
  }
  if (c.cache == null) {
    throw new NullPointerException("No cache returned for name: " + _cacheName);
  }
  return c;
}
 
Example #3
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 #4
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 #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: 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 #7
Source File: CacheConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(Expirations.timeToLiveExpiration(Duration.of(ehcache.getTimeToLiveSeconds(), TimeUnit.SECONDS)))
            .build());
}
 
Example #8
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 #9
Source File: CacheConfiguration.java    From 21-points with Apache License 2.0 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    BeanClassLoaderAwareJCacheRegionFactory.setBeanClassLoader(this.getClass().getClassLoader());
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #10
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 #11
Source File: CacheConfiguration.java    From alchemy with Apache License 2.0 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #12
Source File: PolicyCacheConfiguration.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Configuration getCacheConfiguration() {
    CacheEventListenerConfiguration eventConfig = CacheEventListenerConfigurationBuilder
            .newEventListenerConfiguration(cacheEvent ->
                    LOG.warn("Policy " + ((Policy) cacheEvent.getOldValue()).getId()
                            + " has been evicted. Check your max heap size settings"), EventType.EVICTED)
            .build();
    return Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
            .newCacheConfigurationBuilder(String.class, Policy.class,
                    ResourcePoolsBuilder.heap(numEntries))
            .withSizeOfMaxObjectGraph(2000)
            .withExpiry(Expirations.noExpiration())
            .add(eventConfig)
            .build());
}
 
Example #13
Source File: OntologyCacheConfiguration.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Configuration getCacheConfiguration() {
    if (!StringUtils.isEmpty(repoId)) {
        return new RepositoryConfiguration(String.class, Ontology.class, repoId);
    } else {
        return Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
                .newCacheConfigurationBuilder(String.class, Ontology.class, ResourcePoolsBuilder.heap(numEntries))
                .build());
    }
}
 
Example #14
Source File: CacheConfiguration.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #15
Source File: CacheConfiguration.java    From jhipster-online with Apache License 2.0 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache = jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());

    statisticsJcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(100L))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMinutes(5)))
            .build());
}
 
Example #16
Source File: CacheConfiguration.java    From Full-Stack-Development-with-JHipster with MIT License 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #17
Source File: CacheConfiguration.java    From Spring-5.0-Projects with MIT License 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    BeanClassLoaderAwareJCacheRegionFactory.setBeanClassLoader(this.getClass().getClassLoader());
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #18
Source File: CacheConfiguration.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
public CacheConfiguration(JHipsterProperties jHipsterProperties) {
    JHipsterProperties.Cache.Ehcache ehcache =
        jHipsterProperties.getCache().getEhcache();

    jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
            ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
            .build());
}
 
Example #19
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)));
}