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.

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

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

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        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">
 
    <ehcache:annotation-driven proxy-target-class="true" />
 
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
 
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
 
</beans>
 

3) Configure Ehcache in ehcache.xml.

 
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
 
    <diskStore path="java.io.tmpdir"/>
 
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            overflowToDisk="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="300"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
 
    <cache name="cacheableService"
           maxElementsInMemory="30000"
           eternal="false"
           overflowToDisk="false"
           diskPersistent="false"
           timeToLiveSeconds="86400"
           timeToIdleSeconds="0"
            />
 
</ehcache>
 

4) Use annotations in code:

 
class CacheableService {
 
    @Cacheable(cacheName="cacheableService")
    public MyData getData() {
        ...
        return myData
    }
 
    @Transactional
    @TriggersRemove(cacheName="cacheableService", removeAll=true)
    public void updateData() {
        ...
    }
}
 

No Comments yet »

RSS feed for comments on this post.

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*

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