Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring cache annotation in multiple modules

I have a util module that produces a jar to be used in other applications. I'd like this module to use caching and would prefer to use Spring's annotation-driven caching.

So Util-Module would have something like this:


DataManager.java

...
@Cacheable(cacheName="getDataCache")
public DataObject getData(String key) { ... }
...

data-manager-ehcache.xml

...
<cache name="getDataCache" maxElementsInMemory="100" eternal="true" />
...

data-manager-spring-config.xml

...
<cache:annotation-driven cache-manager="data-manager-cacheManager" />
<!-- ???? --->
<bean id="data-manager-cacheManager" 
    class="org.springframework.cache.ehcache.EhcacheCacheManager" 
    p:cache-manager="data-manager-ehcache"/>
<bean id="data-manager-ehcache" 
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
    p:config-location="data-manager-ehcache.xml"/>
...

I would also like my deployable unit to have caching via Spring annotation, while including the above jar as a dependency. So my Deployable-Unit would have something like this:


MyApp.java

...
@Cacheable(cacheName="getMyAppObjectCache")
public MyAppObject getMyAppObject(String key) { ... }
...

my-app-ehcache.xml

...
<cache name="getMyAppObjectCache" maxElementsInMemory="100" eternal="true" />
...

my-app-spring-config.xml

...
<cache:annotation-driven cache-manager="my-app-cacheManager" />
<!-- ???? --->
<bean id="my-app-cacheManager" 
    class="org.springframework.cache.ehcache.EhcacheCacheManager" 
    p:cache-manager="my-app-ehcache"/>
<bean id="my-app-ehcache" 
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
    p:config-location="my-app-ehcache.xml"/>
...

Question:

Is it possible to use annotation driven caching in both your main project and a dependency module while keeping the configurations separated?

If not, an explanation of why it isn't would be appreciated. If so, an explanation of what needs to change in the above configuration would be appreciated.

like image 383
Snekse Avatar asked Dec 28 '11 16:12

Snekse


People also ask

How do I evict multiple caches in spring?

Spring provides two ways to evict a cache, either by using the @CacheEvict annotation on a method, or by auto-wiring the CacheManger and clearing it by calling the clear() method.

How does Spring cache work internally?

Spring Cache uses the parameters of the method as key and the return value as a value in the cache. When the method is called the first time, Spring will check if the value with the given key is in the cache. It will not be the case, and the method itself will be executed.

What is @EnableCaching in spring boot?

The @EnableCaching annotation triggers a post-processor that inspects every Spring bean for the presence of caching annotations on public methods. If such an annotation is found, a proxy is automatically created to intercept the method call and handle the caching behavior accordingly.


3 Answers

Use this class: http://static.springsource.org/autorepo/docs/spring/3.2.0.M1/api/org/springframework/cache/support/CompositeCacheManager.html like this:

<cache:annotation-driven cache-manager="cacheManager" />  <bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">     <property name="cacheManagers">         <array>             <ref bean="cacheManager1" />             <ref bean="cacheManager2" />         </array>     </property>     <property name="addNoOpCache" value="true" /> </bean> 
like image 135
Nicolas Mommaerts Avatar answered Sep 28 '22 11:09

Nicolas Mommaerts


this seems to be fixed in 3.2M1, see https://jira.springsource.org/browse/SPR-8696

like image 35
bgranvea Avatar answered Sep 28 '22 12:09

bgranvea


Spring currently expects the cacheManager to be a Singleton. This is something that the ehcache-spring-annotations project ran into and I've yet to see the request fulfilled. http://code.google.com/p/ehcache-spring-annotations/issues/detail?id=76

As with all things Java and Spring you do have the option to reimplement the class.

http://forums.terracotta.org/forums/posts/list/5618.page#27960 provides a base explanation of what some people have come up with as a workaround and

  • http://itcb.svn.sourceforge.net/viewvc/itcb/addons/itcb-cache/trunk/
  • http://itcb.svn.sourceforge.net/.../itcb/addons/cachemanager/

Is the actual code they came up with. The approach does create a convention to follow but it would be easy enough to reimplement this with your own version if you do not like the actual approach described.

like image 21
Joe Avatar answered Sep 28 '22 12:09

Joe