Using Spring 3.1's @Cacheable
Before Spring 3.1 I used to use the the @Cacheable annotation provided by the ehcache-spring-annotations project. However with the introduction of Spring Cache, you no longer need to use that library and can use the org.springframework.cache.annotation.Cacheable annotation instead.
To migrate, all you need to do is replace the @Cacheable with the one from spring.
I still needed to maintain a blocking cache which I could easily in the old library, and it was not clear from the Spring documentation how to do that. The solution I went with was, to provide a cacheDecoratorFactory in the ehcache.xml. Something like
<cache name="dataCache" eternal="false"
maxElementsInMemory="0" overflowToDisk="false" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="14400"
memoryStoreEvictionPolicy="LRU" statistics="true">
<cacheDecoratorFactory class="com.saitz.BlockingCacheDecoratorFactory"></cacheDecoratorFactory>
</cache>
where BlockingCacheDecoratorFactory is (in Scala)
class BlockingCacheDecoratorFactory extends CacheDecoratorFactory {
def createDecoratedEhcache(cache: Ehcache, properties: Properties): Ehcache = {
return createBlockingCache(cache);
}
def createDefaultDecoratedEhcache(cache: Ehcache, properties: Properties): Ehcache = {
return createBlockingCache(cache);
}
def createBlockingCache(cache:Ehcache):Ehcache = {
val blockingCache = new BlockingCache(cache);
return blockingCache;
}
}
Written by Zoheb Sait
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Spring mvc
Authors
zohebsait
48.45K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#