Grails and Ehcache Annotations for Spring

Ehcache Annotations for Spring is a project that allows "declarative, aspect based caching to be added to a Spring Framework based application by simple annotation". And because Grails stands on Spring, there is no problem to do that. We are using spring xml based configuration here, but if any one know how rewrite it to Groovy DSL, please leave a comment.

1) Configure dependencies in BuildConfig.groovy.

  1.  
  2. compile('com.googlecode.ehcache-spring-annotations:ehcache-spring-annotations:1.2.0'){
  3. excludes 'ehcache-core', 'slf4j-api'
  4. }
  5.  

2) Configure spring resources.xml. We are using proxy-target-class="true" option to avoid JdkDynamicAopProxy ClassCastException problem.

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  7. http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
  8.  
  9. <ehcache:annotation-driven proxy-target-class="true" />
  10.  
  11. <ehcache:config cache-manager="cacheManager">
  12. <ehcache:evict-expired-elements interval="60" />
  13. </ehcache:config>
  14.  
  15. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
  16.  
  17. </beans>
  18.  

3) Configure Ehcache in ehcache.xml.

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="ehcache.xsd">
  5.  
  6. <diskStore path="java.io.tmpdir"/>
  7.  
  8. <defaultCache
  9. maxElementsInMemory="10000"
  10. eternal="false"
  11. overflowToDisk="false"
  12. timeToIdleSeconds="300"
  13. timeToLiveSeconds="300"
  14. diskPersistent="false"
  15. diskExpiryThreadIntervalSeconds="120"
  16. memoryStoreEvictionPolicy="LRU"
  17. />
  18.  
  19.  
  20. <cache name="cacheableService"
  21. maxElementsInMemory="30000"
  22. eternal="false"
  23. overflowToDisk="false"
  24. diskPersistent="false"
  25. timeToLiveSeconds="86400"
  26. timeToIdleSeconds="0"
  27. />
  28.  
  29. </ehcache>
  30.  
  31.  

4) Use annotations in code:

  1.  
  2. class CacheableService {
  3.  
  4. @Cacheable(cacheName="cacheableService")
  5. public MyData getData() {
  6. ...
  7. return myData
  8. }
  9.  
  10. @Transactional
  11. @TriggersRemove(cacheName="cacheableService", removeAll=true)
  12. public void updateData() {
  13. ...
  14. }
  15. }
  16.  

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.