Java Code Examples for org.ehcache.jsr107.EhcacheCachingProvider#getCacheManager()

The following examples show how to use org.ehcache.jsr107.EhcacheCachingProvider#getCacheManager() . 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: 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 2
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 3
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 4
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 5
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);
}