June 07, 2010

Caching Example with Spring Annotations and Ehcache

« SoapUI : JDBC Request Step example with a Sybase database | Main | XServer Event Handling C Example »

Recently I looked for an easy method caching mechanism. Especially in distributed environments request handling often takes a long time and sooner or later a cache will be necessary. But first how a method cache works.

Method caches, that regards read and write operations, are only useful, if you are implementing a adaptive service to a backend system (e.g. database) and all requests from different clients (read,write) go through your adaptive service. The following picture shows the basic method cache behaviour. Return values of first read method call will be saved in a map. The keys may be several method params (e.g. an id). On the other side write method calls remove the cache objects.

The Ehcache Spring Annotations project enables a method cache. It works with Springframework 3.0 and the developer can use Cacheable and TriggersRemove Annotations to realize a method cache in combination with Ehcache. The following example describes how to use this API.

(1) Data object, that should be cached:
package org.developers.blog.ehcacheexampleproject;

public class Info {
    private Long id;
    public void setId(Long id) {
        this.id = id;
    }
    public Long getId() {
        return this.id;
    }

    private String content;

    public String getContent() {
        return name;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
(2) Service interface:
package org.developers.blog.ehcacheexampleproject;

import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;


public interface InfoService {
    @Cacheable(cacheName="infoCache", keyGeneratorName="infoKeyGenerator")
    Info getInfo(Long id) throws Exception;

    @TriggersRemove(cacheName="infoCache", keyGeneratorName="infoKeyGenerator")
    void updateCustomer(Long id, Info info) throws Exception;
}
(3) Service implementation:
package org.developers.blog.ehcacheexampleproject;

public class InfoServiceImpl implements InfoService {

    public Info getInfo(Long id) throws Exception {
        return persistence.getInfo(id);
    }

    public void updateCustomer(Long id, Info info) throws Exception {
        return persistence.save(id, info);
    }
}
(4) Cache key identifier logic for write and read method:
package org.developers.blog.ehcacheexampleproject;

import com.googlecode.ehcache.annotations.key.CacheKeyGenerator;
import java.io.Serializable;
import org.aopalliance.intercept.MethodInvocation;

public class FirstMethodParamIdHashCodeKeyGenerator implements CacheKeyGenerator { 

    public Serializable generateKey(MethodInvocation methodInvocation) {
        Long id = (Long)methodInvocation.getArguments()[0];
        return id;
    }

}
(5) Spring instrumentalization:


    
    
        
    

    


(6) POM dependencies:

    com.googlecode.ehcache-spring-annotations
    ehcache-spring-annotations
    1.0.3

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 10:12 PM in Java

 

[Trackback URL for this entry]

Comment: Eric Dalquist at Di, 29 Jun 3:09 AM

I am one of the authors of a new project intended to provide Ehcache integration for Spring 2.5 and 3.0 projects via annotations:
http://code.google.com/p/ehcache-spring-annotations/
We are excited to announce the general availability of version 1.1.0
The library provides 2 method-level annotations in the spirit of Spring’s @Transactional:
@Cacheable
@TriggersRemove
When appropriately configured in your Spring application, this project will create caching aspects at runtime around your @Cacheable annotated methods.
Usage documentation can be found on the project wiki:
http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable
http://code.google.com/p/ehcache-spring-annotations/wiki/UsingTriggersRemove

Comment: Priya at Mi, 13 Okt 11:18 AM

If I have a findAllInfo method ,is it possible to pre-load all the data into cache using @postconstruct java annotation.Thanks for the post.

Comment: Jay at Do, 21 Okt 11:20 PM

Hi,

Do you have a simple example of Spring working? I can't seem to get it to cache values locally using a basic desktop application. Do you have a VERY simple example of a desktop app using spring? one/two method app would be great!!

Cheers,
Jay

Trackback: ehcache.net at Di, 28 Dez 3:32 AM

Caching Example with Spring Annotations and Ehcache
Recently I looked for an easy method caching mechanism. Especially in distributed environments request handling often takes a long time and sooner or later a cache will be necessary. But first how a method cache works. <br /> <br />Method caches, that...

Comment: Vincent Louis at Do, 19 Mai 1:47 AM

HI Rafael,

I have some doubts in ehcache, can I ask from here?

Comment: Adeel at Di, 4 Okt 12:21 PM

Very nice.

http://eiconsulting.blogspot.com/2011/10/ehcache-implementation-in-spring.html

Another tutorial with detailed explaination

Comment: Adeel at Di, 4 Okt 12:22 PM

Very nice.

http://eiconsulting.blogspot.com/2011/10/ehcache-implementation-in-spring.html

Another tutorial with detailed explaination

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« June »
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
27282930