/** * Predict the particular {@code Ehcache} implementation that will be returned from * {@link #getObject()} based on logic in {@link #createCache()} and * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}. */ @Override public Class<? extends Ehcache> getObjectType() { if (this.cache != null) { return this.cache.getClass(); } if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return UpdatingSelfPopulatingCache.class; } else { return SelfPopulatingCache.class; } } if (this.blocking) { return BlockingCache.class; } return Cache.class; }
@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(); } }
public static Ehcache createInMemoryBlockingCache(String cacheName, int maxElements) { CacheManager manager = CacheManager.getInstance(); Ehcache cache = getCache(cacheName); if (cache == null) { cache = new Cache(cacheName, maxElements, memoryStoreEvictionPolicy, overflowToDisk, diskStorePath, eternal, timeToLive, timeToIdle, diskPersistent, diskExpiryThreadIntervalSeconds, null); BlockingCache blockingCache = new BlockingCache(cache); manager.addCache(blockingCache); LOG.info("blocking cache created: " + cache.getName()); } return cache; }
/** * Predict the particular {@code Ehcache} implementation that will be returned from * {@link #getObject()} based on logic in {@link #createCache()} and * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}. */ public Class<? extends Ehcache> getObjectType() { if (this.cache != null) { return this.cache.getClass(); } if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return UpdatingSelfPopulatingCache.class; } else { return SelfPopulatingCache.class; } } if (this.blocking) { return BlockingCache.class; } return Cache.class; }
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(); } }
@Test public void cacheDecoratorFactories() { Config config = ConfigFactory .empty() .withValue("cacheDecoratorFactory.blocking.class", fromAnyRef(BlockingCache.class.getName())) .withValue("cacheDecoratorFactory.blocking.p1", fromAnyRef(BlockingCache.class.getName())) .withValue("cacheDecoratorFactory.readT.class", fromAnyRef(ReadThroughCache.class.getName())); CacheConfigurationBuilder builder = new CacheConfigurationBuilder("c1"); CacheConfiguration cache = builder.build(config); List<CacheDecoratorFactoryConfiguration> decorators = cache.getCacheDecoratorConfigurations(); assertEquals(2, decorators.size()); assertEquals(BlockingCache.class.getName(), decorators.get(0).getFullyQualifiedClassPath()); assertEquals(ReadThroughCache.class.getName(), decorators.get(1).getFullyQualifiedClassPath()); }
protected void createCache(String name, int expirationTime) { synchronized (this.getClass()) { cacheManager.addCache(name); Ehcache cache = cacheManager.getEhcache(name); CacheConfiguration config = cache.getCacheConfiguration(); config.setEternal(false); config.setTimeToLiveSeconds(expirationTime); // config.setTimeToIdleSeconds(60); // config.setMaxElementsInMemory(10000); // config.setMaxElementsOnDisk(1000000); BlockingCache blockingCache = new BlockingCache(cache); cacheManager.replaceCacheWithDecoratedCache(cache, blockingCache); } }
/** * Decorate the given Cache, if necessary. * @param cache the raw Cache object, based on the configuration of this FactoryBean * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Ehcache cache) { if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory); } else { return new SelfPopulatingCache(cache, this.cacheEntryFactory); } } if (this.blocking) { return new BlockingCache(cache); } return cache; }
/** * Decorate the given Cache, if necessary. * <p>The default implementation simply returns the given cache object as-is. * * @param cache the raw Cache object, based on the configuration of this FactoryBean * @param model the model containing the name of the cache to retrieve * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Cache cache, EhCacheCachingModel model) { if (model.getCacheEntryFactory() != null) { if (model.getCacheEntryFactory() instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) model.getCacheEntryFactory()); } else { return new SelfPopulatingCache(cache, model.getCacheEntryFactory()); } } if (model.isBlocking()) { return new BlockingCache(cache); } return cache; }
/** * Create and return a reference to {@link Cache} object. * * @return instance of {@link Cache}. */ public static Cache createInstance(String name, net.sf.ehcache.CacheManager cacheManager) { net.sf.ehcache.Ehcache cache = cacheManager.getEhcache(name); if(cache == null) { String error = "createInstance cache: " + name + " is null"; throw new CfgRuntimeException(GlobalErrIds.FT_CACHE_NOT_CONFIGURED, error); } BlockingCache blockingCache = new BlockingCache(cache); blockingCache.setTimeoutMillis(60000); cacheManager.replaceCacheWithDecoratedCache(cache, blockingCache); return new EhCacheImpl(name, blockingCache); }
/** * Create a new reference to the ehcache cache implementation. * * @param cacheName contains the name of the cache to retrieve * @return reference to cache for specified object. */ public Cache getCache( String cacheName ) { Ehcache cache = mEhCacheImpl.getEhcache( cacheName ); if(cache != null) { return new EhCacheImpl( cacheName, new BlockingCache(cache) ); } else { return CacheFactory.createInstance( cacheName, mEhCacheImpl ); } }
/** * Create an instance of a wrapped, singleton cache instance using Ehcache. * * @param name name for the cache instance. * @param blockingCache that is being wrapped. */ EhCacheImpl( String name, BlockingCache blockingCache ) { this.name = name; if ( blockingCache == null ) { String error = " constructor cache: " + name + " is null"; throw new CfgRuntimeException( GlobalErrIds.FT_CACHE_NOT_CONFIGURED, error ); } this.cache = blockingCache; }
@Test public void cacheDecoratorFactory() { Config config = ConfigFactory .empty() .withValue("cacheDecoratorFactory.class", fromAnyRef(BlockingCache.class.getName())); CacheConfigurationBuilder builder = new CacheConfigurationBuilder("c1"); CacheConfiguration cache = builder.build(config); List<CacheDecoratorFactoryConfiguration> decorators = cache.getCacheDecoratorConfigurations(); assertEquals(1, decorators.size()); assertEquals(BlockingCache.class.getName(), decorators.iterator().next() .getFullyQualifiedClassPath()); }
/** * Instantiates a new eh blocking cache. * * @param id the id */ public EhBlockingCache(final String id) { super(id); if (!CACHE_MANAGER.cacheExists(id)) { CACHE_MANAGER.addCache(this.id); Ehcache ehcache = CACHE_MANAGER.getEhcache(this.id); BlockingCache blockingCache = new BlockingCache(ehcache); CACHE_MANAGER.replaceCacheWithDecoratedCache(ehcache, blockingCache); } }
@Override public Ehcache createDefaultDecoratedEhcache(Ehcache cache, Properties properties) { return new BlockingCache(cache); }