Monday 21 September 2015

Implementing spring caching with ehcache....

Implementing spring caching with ehcache....

The Spring caching is in the spring-context.jar, to 
 support Ehcache caching, you need to include the 
 spring-context-support.jar as well.

Step-1
Add Below Dependency for Ehcache.
<dependency>
   <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
     <version>2.9.0</version>
</dependency>

<!-- Spring caching framework inside this -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>

<!-- Support for Ehcache and others -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>




Step-2
 create a xml file as shown in below.
 ehcache.xml (classpath in this example)

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" 
updateCheck="true"
monitoring="autodetect" 
dynamicConfig="true">
<diskStore path="java.io.tmpdir" />

<cache name="movieFindCache" 
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000" 
eternal="false" 
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" 
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>

</ehcache>



Step-3
<!-- Define the cache manager factory instance -->
   <bean id="ehcache"
     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
                p:configLocation="classpath:ehcache.xml" p:shared="true" />


Step-4

<!-- Define the cache manager instance -->
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
                p:cacheManager-ref="ehcache" />

Step-5
<!-- Drive caching via annotations -->
        <cache:annotation-driven cache-manager="cacheManager"
                proxy-target-class="true" />

No comments:

Post a Comment