use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.
the class EhCacheSupportTests method testAcceptExistingCacheManager.
@Test
public void testAcceptExistingCacheManager() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.setCacheManagerName("myCacheManager");
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
assertTrue("Singleton property", cacheManagerFb.isSingleton());
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0);
Cache myCache1 = cm.getCache("myCache1");
assertTrue("No myCache1 defined", myCache1 == null);
EhCacheManagerFactoryBean cacheManagerFb2 = new EhCacheManagerFactoryBean();
cacheManagerFb2.setCacheManagerName("myCacheManager");
cacheManagerFb2.setAcceptExisting(true);
cacheManagerFb2.afterPropertiesSet();
CacheManager cm2 = cacheManagerFb2.getObject();
assertSame(cm, cm2);
cacheManagerFb2.destroy();
} finally {
cacheManagerFb.destroy();
}
}
use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.
the class EhCacheManagerUtils method buildCacheManager.
/**
* Build an EhCache {@link CacheManager} from the default configuration.
* <p>The CacheManager will be configured from "ehcache.xml" in the root of the class path
* (that is, default EhCache initialization - as defined in the EhCache docs - will apply).
* If no configuration file can be found, a fail-safe fallback configuration will be used.
* @param name the desired name of the cache manager
* @return the new EhCache CacheManager
* @throws CacheException in case of configuration parsing failure
*/
public static CacheManager buildCacheManager(String name) throws CacheException {
Configuration configuration = ConfigurationFactory.parseConfiguration();
configuration.setName(name);
return new CacheManager(configuration);
}
use of net.sf.ehcache.CacheManager in project camel by apache.
the class CacheEndpoint method initializeCache.
/**
* Returns {@link Cache} instance or create new one if not exists.
*
* @return {@link Cache}
*/
public Ehcache initializeCache() {
CacheManager cacheManager = getCacheManagerFactory().getInstance();
Ehcache cache;
if (cacheManager.cacheExists(config.getCacheName())) {
if (LOG.isTraceEnabled()) {
LOG.trace("Found an existing cache: {}", config.getCacheName());
LOG.trace("Cache {} currently contains {} elements", config.getCacheName(), cacheManager.getEhcache(config.getCacheName()).getSize());
}
cache = cacheManager.getEhcache(config.getCacheName());
} else {
cache = new Cache(config.getCacheName(), config.getMaxElementsInMemory(), config.getMemoryStoreEvictionPolicy(), config.isOverflowToDisk(), config.getDiskStorePath(), config.isEternal(), config.getTimeToLiveSeconds(), config.getTimeToIdleSeconds(), config.isDiskPersistent(), config.getDiskExpiryThreadIntervalSeconds(), null);
for (CacheEventListener listener : config.getEventListenerRegistry().getEventListeners()) {
cache.getCacheEventNotificationService().registerListener(listener);
}
for (CacheLoaderWrapper loader : config.getCacheLoaderRegistry().getCacheLoaders()) {
loader.init(cache);
cache.registerCacheLoader(loader);
}
cacheManager.addCache(cache);
if (LOG.isDebugEnabled()) {
LOG.debug("Added a new cache: " + cache.getName());
}
}
return cache;
}
use of net.sf.ehcache.CacheManager in project camel by apache.
the class CacheEndpoint method stop.
@Override
public void stop() {
CacheManager cacheManager = getCacheManagerFactory().getInstance();
cacheManager.removeCache(config.getCacheName());
}
use of net.sf.ehcache.CacheManager in project camel by apache.
the class CacheRegistryRefTest method testConfig.
@Test
public void testConfig() throws Exception {
producerTemplate.send(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
Message in = exchange.getIn();
in.setHeader(CacheConstants.CACHE_OPERATION, CacheConstants.CACHE_OPERATION_ADD);
in.setHeader(CacheConstants.CACHE_KEY, "greeting");
in.setBody("Hello World");
}
});
CacheManager cm = cacheEndpoint.getCacheManagerFactory().getInstance();
Cache cache = cm.getCache(cacheEndpoint.getConfig().getCacheName());
Set<CacheEventListener> ehcacheEventListners = cache.getCacheEventNotificationService().getCacheEventListeners();
List<CacheLoader> cacheLoaders = cache.getRegisteredCacheLoaders();
CacheEventListenerRegistry configuredEventRegistry = cacheEndpoint.getConfig().getEventListenerRegistry();
CacheLoaderRegistry configuredLoaderRegistry = cacheEndpoint.getConfig().getCacheLoaderRegistry();
//Test if CacheEventListenerRegistry was referenced correctly
assertEquals("CacheEventListenerRegistry size", 1, configuredEventRegistry.size());
//Expecting 1 loader to be configured via spring
assertEquals("configuredLoaderRegistry size", 1, configuredLoaderRegistry.size());
//Expecting 1 listener added by us: TestCacheEventListener
assertEquals("Number of registered listeners", 1, ehcacheEventListners.size());
assertEquals("Number of registered loaders", 1, cacheLoaders.size());
//Is our TestCacheEventListener really invoked?
int puts = 0;
for (Object listener : ehcacheEventListners) {
if (listener instanceof TestCacheEventListener) {
puts = ((TestCacheEventListener) listener).getNumberOfPuts();
break;
}
}
assertEquals("TestCacheEventListener put invocations", 1, puts);
//Is cache loader initialized by ehcache
assertEquals("loader initialized", cacheLoaders.get(0).getStatus(), Status.STATUS_ALIVE);
}
Aggregations