Search in sources :

Example 31 with Ehcache

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;
}
Also used : Ehcache(net.sf.ehcache.Ehcache)

Example 32 with Ehcache

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;
}
Also used : CacheEventListener(net.sf.ehcache.event.CacheEventListener) CacheManager(net.sf.ehcache.CacheManager) Ehcache(net.sf.ehcache.Ehcache) Cache(net.sf.ehcache.Cache)

Example 33 with Ehcache

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);
}
Also used : EhCacheBasedUserCache(org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache) Ehcache(net.sf.ehcache.Ehcache) Test(org.junit.Test)

Example 34 with Ehcache

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();
        }
    }
}
Also used : Ehcache(net.sf.ehcache.Ehcache) ClassPathResource(org.springframework.core.io.ClassPathResource) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) BlockingCache(net.sf.ehcache.constructs.blocking.BlockingCache) UpdatingSelfPopulatingCache(net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache) Cache(net.sf.ehcache.Cache)

Example 35 with Ehcache

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();
    }
}
Also used : BlockingCache(net.sf.ehcache.constructs.blocking.BlockingCache) CacheManager(net.sf.ehcache.CacheManager) Ehcache(net.sf.ehcache.Ehcache) Test(org.junit.Test)

Aggregations

Ehcache (net.sf.ehcache.Ehcache)39 Test (org.junit.Test)14 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)9 Element (net.sf.ehcache.Element)8 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)7 CacheException (net.sf.ehcache.CacheException)5 CacheManager (net.sf.ehcache.CacheManager)5 CachedResource (org.apereo.portal.utils.cache.resource.CachedResource)5 CachingResourceLoaderImpl (org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl)5 LoadedResource (org.apereo.portal.utils.cache.resource.LoadedResource)5 FileSystemResource (org.springframework.core.io.FileSystemResource)5 Resource (org.springframework.core.io.Resource)5 CacheKey (org.apereo.portal.utils.cache.CacheKey)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 Test (org.testng.annotations.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3