Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ehcache Cache Server + BlockingCache ?

Tags:

ehcache

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.

like image 573
Prem Avatar asked Aug 08 '26 11:08

Prem


2 Answers

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.

like image 177
Nick Avatar answered Aug 11 '26 06:08

Nick


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.

like image 36
Alex Snaps Avatar answered Aug 11 '26 06:08

Alex Snaps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!