Search in sources :

Example 6 with CacheManager

use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.

the class EhCacheCacheManagerTests method setup.

@Before
public void setup() {
    nativeCacheManager = new CacheManager(new Configuration().name("EhCacheCacheManagerTests").defaultCache(new CacheConfiguration("default", 100)));
    addNativeCache(CACHE_NAME);
    cacheManager = new EhCacheCacheManager(nativeCacheManager);
    cacheManager.setTransactionAware(false);
    cacheManager.afterPropertiesSet();
    transactionalCacheManager = new EhCacheCacheManager(nativeCacheManager);
    transactionalCacheManager.setTransactionAware(true);
    transactionalCacheManager.afterPropertiesSet();
}
Also used : CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Configuration(net.sf.ehcache.config.Configuration) CacheManager(net.sf.ehcache.CacheManager) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Before(org.junit.Before)

Example 7 with CacheManager

use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.

the class EhCacheSupportTests method testCacheManagerFromConfigFile.

public void testCacheManagerFromConfigFile() throws Exception {
    EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
    cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
    cacheManagerFb.setCacheManagerName("myCacheManager");
    cacheManagerFb.afterPropertiesSet();
    try {
        CacheManager cm = cacheManagerFb.getObject();
        assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1);
        Cache myCache1 = cm.getCache("myCache1");
        assertFalse("myCache1 is not eternal", myCache1.getCacheConfiguration().isEternal());
        assertTrue("myCache1.maxElements == 300", myCache1.getCacheConfiguration().getMaxEntriesLocalHeap() == 300);
    } finally {
        cacheManagerFb.destroy();
    }
}
Also used : CacheManager(net.sf.ehcache.CacheManager) ClassPathResource(org.springframework.core.io.ClassPathResource) 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 8 with CacheManager

use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.

the class EhCacheSupportTests method testCacheManagerConflict.

@Test
public void testCacheManagerConflict() 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.afterPropertiesSet();
        fail("Should have thrown CacheException because of naming conflict");
    } catch (CacheException ex) {
    // expected
    } finally {
        cacheManagerFb.destroy();
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) CacheManager(net.sf.ehcache.CacheManager) 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) Test(org.junit.Test)

Example 9 with CacheManager

use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.

the class EhCacheSupportTests method testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache.

@Test
public void testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache() throws Exception {
    EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
    cacheManagerFb.afterPropertiesSet();
    try {
        CacheManager cm = cacheManagerFb.getObject();
        EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
        cacheFb.setCacheManager(cm);
        cacheFb.setCacheName("myCache1");
        cacheFb.setCacheEntryFactory(new UpdatingCacheEntryFactory() {

            @Override
            public Object createEntry(Object key) throws Exception {
                return key;
            }

            @Override
            public void updateEntryValue(Object key, Object value) throws Exception {
            }
        });
        assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class);
        cacheFb.afterPropertiesSet();
        Ehcache myCache1 = cm.getEhcache("myCache1");
        assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache);
        assertEquals("myKey1", myCache1.get("myKey1").getObjectValue());
    } finally {
        cacheManagerFb.destroy();
    }
}
Also used : UpdatingCacheEntryFactory(net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory) UpdatingSelfPopulatingCache(net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache) CacheManager(net.sf.ehcache.CacheManager) Ehcache(net.sf.ehcache.Ehcache) CacheException(net.sf.ehcache.CacheException) Test(org.junit.Test)

Example 10 with CacheManager

use of net.sf.ehcache.CacheManager in project spring-framework by spring-projects.

the class EhCacheSupportTests method testEhCacheFactoryBeanWithSelfPopulatingCache.

@Test
public void testEhCacheFactoryBeanWithSelfPopulatingCache() throws Exception {
    EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
    cacheManagerFb.afterPropertiesSet();
    try {
        CacheManager cm = cacheManagerFb.getObject();
        EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
        cacheFb.setCacheManager(cm);
        cacheFb.setCacheName("myCache1");
        cacheFb.setCacheEntryFactory(new CacheEntryFactory() {

            @Override
            public Object createEntry(Object key) throws Exception {
                return key;
            }
        });
        assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class);
        cacheFb.afterPropertiesSet();
        Ehcache myCache1 = cm.getEhcache("myCache1");
        assertTrue(myCache1 instanceof SelfPopulatingCache);
        assertEquals("myKey1", myCache1.get("myKey1").getObjectValue());
    } finally {
        cacheManagerFb.destroy();
    }
}
Also used : SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) UpdatingSelfPopulatingCache(net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache) UpdatingCacheEntryFactory(net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory) CacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory) CacheManager(net.sf.ehcache.CacheManager) Ehcache(net.sf.ehcache.Ehcache) CacheException(net.sf.ehcache.CacheException) Test(org.junit.Test)

Aggregations

CacheManager (net.sf.ehcache.CacheManager)102 Cache (net.sf.ehcache.Cache)55 ClassPathResource (org.springframework.core.io.ClassPathResource)21 Element (net.sf.ehcache.Element)20 Configuration (net.sf.ehcache.config.Configuration)18 Test (org.junit.Test)18 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)17 MarkupCache (org.apache.wicket.markup.MarkupCache)10 CacheException (net.sf.ehcache.CacheException)9 IOException (java.io.IOException)7 Ehcache (net.sf.ehcache.Ehcache)7 UpdatingSelfPopulatingCache (net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache)6 URL (java.net.URL)5 BlockingCache (net.sf.ehcache.constructs.blocking.BlockingCache)5 SelfPopulatingCache (net.sf.ehcache.constructs.blocking.SelfPopulatingCache)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)4 PersistenceConfiguration (net.sf.ehcache.config.PersistenceConfiguration)4 Around (org.aspectj.lang.annotation.Around)4 Before (org.junit.Before)4 File (java.io.File)3