Product Documentation : Ehcache Developer Guide : Cache Decorators : Adding Decorated Caches to a CacheManager
Adding Decorated Caches to a CacheManager
Having created a decorator programmatically, it is generally useful to put it in a place where multiple threads can access it. Note that decorators created via configuration in ehcache.xml have already been added to the CacheManager.
Using CacheManager.replaceCacheWithDecoratedCache()
A built-in way is to replace the Cache in CacheManager with the decorated one. This is achieved as in the following example:
cacheManager.replaceCacheWithDecoratedCache(cache, newBlockingCache);
The CacheManager.replaceCacheWithDecoratedCache( ) method requires that the decorated cache be built from the underlying cache from the same name.
Note that any overridden Ehcache methods will take on new behaviors without casting, as per the normal rules of Java. Casting is only required for new methods that the decorator introduces.
Any calls to get the cache out of the CacheManager now return the decorated one.
A word of caution. This method should be called in an appropriately synchronized init style method before multiple threads attempt to use it. All threads must be referencing the same decorated cache. An example of a suitable init method is found in CachingFilter:
/**
* The cache holding the web pages. Ensure that all threads for a given cache
* name are using the same instance of this.
*/
private BlockingCache blockingCache;
/**
* Initialises blockingCache to use
*
* @throws CacheException The most likely cause is that a cache has not been
* configured in Ehcache's configuration file ehcache.xml
* for the filter name
*/
public void doInit() throws CacheException {
synchronized (this.getClass()) {
if (blockingCache == null) {
final String cacheName = getCacheName();
Ehcache cache = getCacheManager().getEhcache(cacheName);
if (!(cache instanceof BlockingCache)) {
//decorate and substitute
BlockingCache newBlockingCache = new BlockingCache(cache);
getCacheManager().replaceCacheWithDecoratedCache(cache, newBlockingCache);
}
blockingCache = (BlockingCache) getCacheManager().getEhcache(getCacheName());
}
}
}
Ehcache blockingCache = singletonManager.getEhcache("sampleCache1");
The returned cache will exhibit the decorations.
Using CacheManager.addDecoratedCache()
Sometimes you want to add a decorated cache but retain access to the underlying cache.
The way to do this is to create a decorated cache and then call cache.setName(new_name) and then add it to CacheManager with CacheManager.addDecoratedCache().
/**
* Adds a decorated {@link Ehcache} to the CacheManager. This method neither
* creates the memory/disk store nor initializes the cache. It only adds the
* cache reference to the map of caches held by this cacheManager.
*
* It is generally required that a decorated cache, once constructed, is made
* available to other execution threads. The simplest way of doing this is to
* either add it to the cacheManager with a different name or substitute the
* original cache with the decorated one.
*
* This method adds the decorated cache assuming it has a different name.
* If another cache (decorated or not) with the same name already exists,
* it will throw {@link ObjectExistsException}. For replacing existing
* cache with another decorated cache having same name, please use
* {@link #replaceCacheWithDecoratedCache(Ehcache, Ehcache)}
*
* Note that any overridden Ehcache methods by the decorator will take on
* new behaviours without casting. Casting is only required for new methods
* that the decorator introduces. For more information see the well known
* Gang of Four Decorator pattern.
*
* @param decoratedCache
* @throws ObjectExistsException
* if another cache with the same name already exists.
*/
public void addDecoratedCache(Ehcache decoratedCache) throws ObjectExistsException {
Copyright © 2014 Software AG, Darmstadt, Germany.

Product Logo |   Feedback