Reputation: 113
Is it possible to use Ehcache Cache Server and have it be configured with blockingCache ? I cant seem to find how to configure this in the ehcache.xml file... only programatically.
Upvotes: 3
Views: 2273
Reputation: 344
To use BlockingCache as the default decorator for your cache via ehcache.xml, first you should implement your own CacheDecoratorFactory, say it's DefaultCacheDecoratorFactory:
public class DefaultCacheDecoratorFactory extends CacheDecoratorFactory {
@Override
public Ehcache createDecoratedEhcache(Ehcache cache, Properties properties) {
return new BlockingCache(cache);
}
@Override
public Ehcache createDefaultDecoratedEhcache(Ehcache cache, Properties properties) {
return new BlockingCache(cache);
}
}
then configure it as part of your cache definition, like this:
<cache name="CACHE_NAME" more values here.../>
<cacheDecoratorFactory class="whatsoever.DefaultCacheDecoratorFactory"/>
</cache>
And using cacheManager.getEhCache() to access the cache other than cacheManager.getCache(), because it only returns null for your decorated cache.
Upvotes: 9
Reputation: 1250
You can declare decorated caches programmatically, but also in configuration, see: http://ehcache.org/documentation/apis/cache-decorators#by-configuration
You'd need to add a net.sf.ehcache.constructs.CacheDecoratorFactory implementation that does what you need. I guess in you're case it could do some pattern matching against the Ehcache instance passed to the net.sf.ehcache.constructs.CacheDecoratorFactory#createDecoratedEhcache and return either null, or the cache instance decorated by the BlockingCache.
Word of caution though, is to make sure that on misses, you always put back (even null) into the cache, otherwise the write lock for that key/segment won't be unlocked.
Upvotes: 1