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" />

Implementing Spring caching in project @ Service Layer

Implementing Spring caching in project

STEP-1
Enabling Spring caching annotation:-

Define spring cache manager and enable the cache annotation in meta-configuration file

<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="false"/>


STEP-2
Defining Spring cache manager..

<!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
           <set>
  <bean class="org.springframework.cache.concurrent.
ConcurrentMapCacheFactoryBean" p:name="spring-mvcCache"/>
           </set>
</property>

</bean>

Note : Cache name is spring-mvcCache


STEP-3
Applying annotation on service layer...................

    @Cacheable(value="quote", key="#quoteNumber")
     public Quote getQuote(String quoteNumber) {
           Quote retrievedQuote = new Quote(quoteNumber);
           try {
            retrievedQuote = quoteRetrieveSAO.retrieve(quoteNumber);
          } catch (RetrieveException e) {
    log.fatal("Failed to retrieve quote from cache for quote number:" + quoteNumber, e);
           }
           return retrievedQuote;
     }

     @CachePut(value="quote", key="#quoteNumber")
     public Quote updateQuote(String quoteNumber, Quote quote) {
           return quote;
     }

     @Cacheable(value="quoteErrors", key="#quoteNumber")
     public List<CollectionSetError> getQuoteErrors(String quoteNumber) {
           return new ArrayList<CollectionSetError>();
     }


     @Override
     @CacheEvict(value="spring-mvcCache",allEntries=true)
     public void deleteAlbum(int albumId) {
           String sql = "delete from albums WHERE albumid = " + albumId + " ";
           getJdbcTemplate().update(sql);
     }






Message Level Security with UserName Token Example!!!






Friday 18 September 2015