Introduction

by:http://opensource.atlassian.com/confluence/spring/display/DISC/AOP+Cache

I've written an AOP interceptor which allows you to specify which methods to cache for Spring beans.
Different cache providers are available: Memory HashMap, EHCache, OSCache (which is clusterable) and SwarmCache.

Example

We start by defining a bookManager object which has one method

 

Next we define the AOP interceptor which will cache the results. This example will return a cached result (the List of Books) instead of a call to bookManager.getRelated(Book), if the method is called with the same Book argument.

Available implementations of CacheInterceptor are:

The cache in the example is expired after 15 minutes.  
     

< bean  id ="cacheInterceptor"  
    class
="org.springframework.aop.interceptor.cache.OSCacheInterceptor" >
    
< property  name ="refreshPeriods" >
        
< props >
            
<!--  Cache the returned related books for 15 minutes  -->
            
< prop  key ="com.example.BookManager@getRelated" > 900 </ prop >
        
</ props >
    
</ property >
    
<!--  For caches not defined under 'refreshPeriods', use this value  -->
    
< property  name ="defaultRefreshPeriod" >
        
< value > -1 </ value >
    
</ property >
    
<!--  Which method to call for non-standard objects like String, Boolean or Number.
 This method should be a simple method, like getId() or toString(), but should
 uniquely identify the object. 
-->
    
< property  name ="identifiers" >
        
< props >
            
<!--  If a method contains an argument com.example.Book,
             the generated cache key contains the value of Book.getId() 
-->
            
< prop  key ="com.example.Book" > getId </ prop >
        
</ props >
    
</ property >
</ bean >

      

refreshPeriods and defaultRefreshPeriod are properties that are specific for the OSCacheInterceptor. The best way to know how to configure a specific CacheInterceptor implementation is by having a look in the Javadoc (see the project.zip file).

refreshPeriods indicates how long the results will be cached in seconds. So a call to com.example.BookManager.getRelated() will be cached for 900 seconds (15 minutes).
When a method is intercepted that is not defined under refreshPeriods, the value of defaultRefreshPeriod will be used.

In order to be able to identify a call to the method with the same parameter, we use the identifiers property. Here you can list the function that needs to be called in order to get a unique identifier for this class. For each class that is used as an argument for the cached methods, specify the method name (which may not have any parameters). This is a better aproach than using the toString() method, since this method can produce long lines while most of the time a simple identifier is available. So in this example, book.getId() will be used to identify separate Book arguments. There is no need to specify arguments that are primitives (float, int), Strings or Numbers (Float, Integer, ...).

Now we wire the cacheInterceptor to the bookManager bean and we're done! Calls to bookManager.getRelated() will from now on be cached for 15 minutes.
Of course you can add as many beans to the cache as you want.

<!--  An advisor that wraps bookManager.getRelated() with the cacheInterceptor  -->
< bean  id ="bookManagerAdvisor"  
    class
="org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
    
< property  name ="advice" >
        
< ref  bean ="cacheInterceptor" />
    
</ property >
    
< property  name ="patterns" >
        
< list >
            
< value > .*getRelated </ value >
        
</ list >
    
</ property >
</ bean >
< bean  id ="bookManagerCacheProxyCreator"  
    class
="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
    
< property  name ="beanNames" >
        
< value > bookManager </ value >
    
</ property >
    
< property  name ="interceptorNames" >
        
< list >
            
< value > bookManagerAdvisor </ value >
        
</ list >
    
</ property >
</ bean >