org.ehcache.jsr107.EhcacheCachingProvider Java Examples

The following examples show how to use org.ehcache.jsr107.EhcacheCachingProvider. 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 ehcache3-samples with Apache License 2.0 6 votes vote down vote up
private CacheManager createInMemoryCacheManager() {
    long cacheSize = jHipsterProperties.getCache().getEhcache().getMaxEntries();
    long ttl = jHipsterProperties.getCache().getEhcache().getTimeToLiveSeconds();

    org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
        .newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder
            .heap(cacheSize))
        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ttl)))
        .build();

    Map<String, org.ehcache.config.CacheConfiguration<?, ?>> caches = createCacheConfigurations(cacheConfiguration);

    EhcacheCachingProvider provider = getCachingProvider();
    DefaultConfiguration configuration = new DefaultConfiguration(caches, getClassLoader());
    return getCacheManager(provider, configuration);
}
 
Example #2
Source File: JCacheCalculationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Before
public void before() throws Exception {
  CachingProvider cachingProvider = Caching.getCachingProvider();
  EhcacheCachingProvider ehcacheProvider = (EhcacheCachingProvider) cachingProvider;

  DefaultConfiguration configuration = new DefaultConfiguration(ehcacheProvider.getDefaultClassLoader(),
    new DefaultPersistenceConfiguration(diskPath.newFolder()));

  CacheConfiguration<Integer, String> cacheConfiguration =
    CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, resources).build();
  configuration.addCacheConfiguration("cache", cacheConfiguration);

  cacheManager = ehcacheProvider.getCacheManager(ehcacheProvider.getDefaultURI(), configuration);

  EhcacheManager ehcacheManager = cacheManager.unwrap(EhcacheManager.class);
  Field field = EhcacheManager.class.getDeclaredField("serviceLocator");
  field.setAccessible(true);
  @SuppressWarnings("unchecked")
  ServiceProvider<Service> serviceProvider = (ServiceProvider<Service>)field.get(ehcacheManager);
  StatisticsService statisticsService = serviceProvider.getService(StatisticsService.class);

  cache = cacheManager.getCache("cache", Integer.class, String.class);

  cacheStatistics = statisticsService.getCacheStatistics("cache");
}
 
Example #3
Source File: CacheConfiguration.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public javax.cache.CacheManager jCacheCacheManager() {
    CachingProvider provider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
    EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) provider;
    return ehcacheCachingProvider.getCacheManager(
            ehcacheCachingProvider.getDefaultURI(),
            createConfig(provider.getDefaultClassLoader()));
}
 
Example #4
Source File: Application.java    From conciliator with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public CacheManager cacheManager(@Autowired Config config) {
    long ttl = Long.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_TTL));

    config.getProperties().getProperty(Config.PROP_CACHE_SIZE);

    MemSize memSize = MemSize.valueOf(config.getProperties().getProperty(Config.PROP_CACHE_SIZE));

    LogFactory.getLog(getClass()).info(
            String.format("Initializing cache TTL=%d secs, size=%d %s",
                    ttl, memSize.getSize(), memSize.getUnit().toString()));

    org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Object.class, Object.class,
                    ResourcePoolsBuilder.newResourcePoolsBuilder()
                            .heap(memSize.getSize(), memSize.getUnit()))
            .withExpiry(Expirations.timeToLiveExpiration(new org.ehcache.expiry.Duration(ttl, TimeUnit.SECONDS)))
            .build();

    Map<String, CacheConfiguration<?, ?>> caches = new HashMap<>();
    caches.put(CACHE_DEFAULT, cacheConfiguration);

    EhcacheCachingProvider provider = (EhcacheCachingProvider) javax.cache.Caching.getCachingProvider();

    // when our cacheManager bean is re-created several times for
    // diff test configurations, this provider seems to hang on to state
    // causing cache settings to not be right. so we always close().
    provider.close();

    DefaultConfiguration configuration = new DefaultConfiguration(
            caches, provider.getDefaultClassLoader());

    return new JCacheCacheManager(
            provider.getCacheManager(provider.getDefaultURI(), configuration));
}
 
Example #5
Source File: CacheConfiguration.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
private CacheManager createClusteredCacheManager() {
    ApplicationProperties.Cluster clusterProperties = applicationProperties.getCluster();
    URI clusterUri = clusterProperties.getUri();
    boolean autoCreate = clusterProperties.isAutoCreate();
    long clusteredCacheSize = clusterProperties.getSizeInMb();
    String offheapResourceName = clusterProperties.getOffheapResourceName();
    Consistency consistency = clusterProperties.getConsistency();

    long heapCacheSize = jHipsterProperties.getCache().getEhcache().getMaxEntries();
    long ttl = jHipsterProperties.getCache().getEhcache().getTimeToLiveSeconds();

    ClusteringServiceConfigurationBuilder clusteringServiceConfigurationBuilder = ClusteringServiceConfigurationBuilder.cluster(clusterUri);
    ServerSideConfigurationBuilder serverSideConfigurationBuilder = (autoCreate ? clusteringServiceConfigurationBuilder.autoCreate() : clusteringServiceConfigurationBuilder.expecting())
        .defaultServerResource(offheapResourceName);

    org.ehcache.config.CacheConfiguration<Object, Object> cacheConfiguration = CacheConfigurationBuilder
        .newCacheConfigurationBuilder(Object.class, Object.class, ResourcePoolsBuilder
            .heap(heapCacheSize)
            .with(ClusteredResourcePoolBuilder.clusteredDedicated(clusteredCacheSize, MemoryUnit.MB)))
        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ttl)))
        .add(new ClusteredStoreConfiguration(consistency)).build();

    Map<String, org.ehcache.config.CacheConfiguration<?, ?>> caches = createCacheConfigurations(cacheConfiguration);

    EhcacheCachingProvider provider = getCachingProvider();
    DefaultConfiguration configuration = new DefaultConfiguration(caches, getClassLoader(), serverSideConfigurationBuilder.build());
    return getCacheManager(provider, configuration);
}
 
Example #6
Source File: EhCacheManagerProvider.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private CacheManager create(@Nullable final URI config) {
  CachingProvider provider = Caching.getCachingProvider(
      EhcacheCachingProvider.class.getName(),
      EhcacheCachingProvider.class.getClassLoader()
  );

  log.info("Creating cache-manager with configuration: {}", config);
  CacheManager manager = provider.getCacheManager(config, getClass().getClassLoader());
  log.debug("Created cache-manager: {}", manager);
  return manager;
}
 
Example #7
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCacheManagerLevelConfiguration() throws Exception {
  // tag::ehcacheCacheManagerConfigurationExample[]
  CachingProvider cachingProvider = Caching.getCachingProvider();
  EhcacheCachingProvider ehcacheProvider = (EhcacheCachingProvider) cachingProvider; // <1>

  DefaultConfiguration configuration = new DefaultConfiguration(ehcacheProvider.getDefaultClassLoader(),
    new DefaultPersistenceConfiguration(getPersistenceDirectory())); // <2>

  CacheManager cacheManager = ehcacheProvider.getCacheManager(ehcacheProvider.getDefaultURI(), configuration); // <3>
  // end::ehcacheCacheManagerConfigurationExample[]

  assertThat(cacheManager, notNullValue());
}
 
Example #8
Source File: EHCache3Test.java    From cache2k-benchmark with Apache License 2.0 5 votes vote down vote up
@Test
public void testJCacheDefault10000Configuration() {
	JCacheCacheFactory f = new JCacheCacheFactory();
	f.setProvider(EhcacheCachingProvider.class.getName());
	// f.setCacheName("benchmark");
	BenchmarkCache<Integer, Integer> _cache = f.create(10000);
	_cache.put(1, 3);
}
 
Example #9
Source File: EHCache3Test.java    From cache2k-benchmark with Apache License 2.0 5 votes vote down vote up
@Test
public void testJCacheDefault10000ConfigurationWithSource() {
	JCacheCacheFactory f = new JCacheCacheFactory();
	f.setProvider(EhcacheCachingProvider.class.getName());
	// f.setCacheName("benchmark");
	BenchmarkCache<Integer, Integer> _cache = f.createLoadingCache(Integer.class, Integer.class, 10000, new BenchmarkCacheLoader<Integer, Integer>() {
		@Override
		public Integer load(final Integer key) {
			return key;
		}
	});
	assertEquals(3, (int) _cache.get(3));
}
 
Example #10
Source File: CacheUsage.java    From ehcache3-samples with Apache License 2.0 4 votes vote down vote up
private static EhcacheCachingProvider getCachingProvider() {
  return (EhcacheCachingProvider) Caching.getCachingProvider();
}
 
Example #11
Source File: CacheConfiguration.java    From ehcache3-samples with Apache License 2.0 4 votes vote down vote up
private CacheManager getCacheManager(EhcacheCachingProvider provider, DefaultConfiguration configuration) {
    return provider.getCacheManager(provider.getDefaultURI(), configuration);
}
 
Example #12
Source File: CacheConfiguration.java    From ehcache3-samples with Apache License 2.0 4 votes vote down vote up
private EhcacheCachingProvider getCachingProvider() {
    return (EhcacheCachingProvider) Caching.getCachingProvider();
}