Search in sources :

Example 61 with CacheManager

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();
    }
}
Also used : 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 62 with CacheManager

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

Example 63 with CacheManager

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

Example 64 with CacheManager

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

Example 65 with CacheManager

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);
}
Also used : CacheEventListener(net.sf.ehcache.event.CacheEventListener) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) CacheException(net.sf.ehcache.CacheException) Exchange(org.apache.camel.Exchange) CacheManager(net.sf.ehcache.CacheManager) CacheLoader(net.sf.ehcache.loader.CacheLoader) Cache(net.sf.ehcache.Cache) Test(org.junit.Test) BaseCacheTest(org.apache.camel.component.BaseCacheTest)

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