use of net.sf.ehcache.Ehcache in project camel by apache.
the class CacheValidate method isValid.
public boolean isValid(CacheManager cacheManager, String cacheName, String key) {
LOG.trace("Cache Name: {}", cacheName);
if (!cacheManager.cacheExists(cacheName)) {
LOG.debug("No existing Cache found with name: {}" + ". Please ensure a cache is first instantiated using a Cache Consumer or Cache Producer." + " Replacement will not be performed since the cache {} does not presently exist", cacheName, cacheName);
return false;
}
LOG.debug("Found an existing cache: {}", cacheName);
if (LOG.isTraceEnabled()) {
LOG.trace("Cache {} currently contains {} elements", cacheName, cacheManager.getCache(cacheName).getSize());
}
Ehcache cache = cacheManager.getCache(cacheName);
if (!cache.isKeyInCache(key)) {
LOG.debug("No Key with name: {} presently exists in the cache. It is also possible that the key may have expired in the cache." + " Replacement will not be performed until an appropriate key/value pair is added to (or) found in the cache.", key);
return false;
}
return true;
}
use of net.sf.ehcache.Ehcache 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.Ehcache in project spring-security by spring-projects.
the class EhCacheBasedUserCacheTests method startupDetectsMissingCache.
@Test(expected = IllegalArgumentException.class)
public void startupDetectsMissingCache() throws Exception {
EhCacheBasedUserCache cache = new EhCacheBasedUserCache();
cache.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
Ehcache myCache = getCache();
cache.setCache(myCache);
assertThat(cache.getCache()).isEqualTo(myCache);
}
use of net.sf.ehcache.Ehcache in project spring-framework by spring-projects.
the class EhCacheSupportTests method doTestEhCacheFactoryBean.
private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exception {
Cache cache;
EhCacheManagerFactoryBean cacheManagerFb = null;
boolean cacheManagerFbInitialized = false;
try {
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
Class<? extends Ehcache> objectType = cacheFb.getObjectType();
assertTrue(Ehcache.class.isAssignableFrom(objectType));
assertTrue("Singleton property", cacheFb.isSingleton());
if (useCacheManagerFb) {
cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
cacheManagerFb.setCacheManagerName("cache");
cacheManagerFb.afterPropertiesSet();
cacheManagerFbInitialized = true;
cacheFb.setCacheManager(cacheManagerFb.getObject());
}
cacheFb.setCacheName("myCache1");
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
Class<? extends Ehcache> objectType2 = cacheFb.getObjectType();
assertSame(objectType, objectType2);
CacheConfiguration config = cache.getCacheConfiguration();
assertEquals("myCache1", cache.getName());
if (useCacheManagerFb) {
assertEquals("myCache1.maxElements", 300, config.getMaxEntriesLocalHeap());
} else {
assertEquals("myCache1.maxElements", 10000, config.getMaxEntriesLocalHeap());
}
// Cache region is not defined. Should create one with default properties.
cacheFb = new EhCacheFactoryBean();
if (useCacheManagerFb) {
cacheFb.setCacheManager(cacheManagerFb.getObject());
}
cacheFb.setCacheName("undefinedCache");
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
config = cache.getCacheConfiguration();
assertEquals("undefinedCache", cache.getName());
assertTrue("default maxElements is correct", config.getMaxEntriesLocalHeap() == 10000);
assertFalse("default eternal is correct", config.isEternal());
assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 120);
assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 120);
assertTrue("default diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 120);
// overriding the default properties
cacheFb = new EhCacheFactoryBean();
if (useCacheManagerFb) {
cacheFb.setCacheManager(cacheManagerFb.getObject());
}
cacheFb.setBeanName("undefinedCache2");
cacheFb.setMaxEntriesLocalHeap(5);
cacheFb.setTimeToLive(8);
cacheFb.setTimeToIdle(7);
cacheFb.setDiskExpiryThreadIntervalSeconds(10);
cacheFb.afterPropertiesSet();
cache = (Cache) cacheFb.getObject();
config = cache.getCacheConfiguration();
assertEquals("undefinedCache2", cache.getName());
assertTrue("overridden maxElements is correct", config.getMaxEntriesLocalHeap() == 5);
assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 8);
assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 7);
assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 10);
} finally {
if (cacheManagerFbInitialized) {
cacheManagerFb.destroy();
} else {
CacheManager.getInstance().shutdown();
}
}
}
use of net.sf.ehcache.Ehcache in project spring-framework by spring-projects.
the class EhCacheSupportTests method testEhCacheFactoryBeanWithBlockingCache.
@Test
public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
cacheManagerFb.afterPropertiesSet();
try {
CacheManager cm = cacheManagerFb.getObject();
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
cacheFb.setCacheManager(cm);
cacheFb.setCacheName("myCache1");
cacheFb.setBlocking(true);
assertEquals(cacheFb.getObjectType(), BlockingCache.class);
cacheFb.afterPropertiesSet();
Ehcache myCache1 = cm.getEhcache("myCache1");
assertTrue(myCache1 instanceof BlockingCache);
} finally {
cacheManagerFb.destroy();
}
}
Aggregations