-
Notifications
You must be signed in to change notification settings - Fork 2
Home
How To Guide for Cache annotations
Overview This project provide a means of caching at method level with annotations. The actual caching implementation is independent of the annotations allowing you to plug in for example a simple memory based cache, an ehache based cache or a memcached based cache. By implementing uk.ltd.woodsideconsultancy.aop.cache.CacheInterface you can also implement your own caching. Using the component
Integrating with your build
Examples below assume you are using Maven to build your project.
Dependencies Add the following dependency to your impl project pom.
<dependency>
<groupId>uk.ltd.woodsideconsultancy.aop</groupId>
<artifactId>cache-aop</artifactId>
<version>0.1</version>
<scope>compile</scope>
</dependency>
Please update version info according to release then add which ever cache implementation you require:-
-
a) For projects requiring memcached based caching.
uk.ltd.woodsideconsultancy.aop cache-impl-memcached 0.1 compile -
b) For projects requiring ehcache based caching
uk.ltd.woodsideconsultancy.aop cache-impl-ehcache 0.1 compile -
c) For projects requiring a simple memory based cache. Please note there is no limit applied to the size of this cache implementation.
uk.ltd.woodsideconsultancy.aop cache-impl-simple 0.1 provided
JDK Make sure your pom specifies java 1.5 or later as the build JDK. For example specify 1.5 or later in your pom
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
Compilation
Add the aspect to your compilation phase of the impl project, this requires adding aspectj as a dependency. For example add the following is in the impl pom
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<complianceLevel>1.5</complianceLevel>
<weaveDependencies>
<weaveDependency>
<groupId>uk.ltd.woodsideconsultancy.aop</groupId>
<artifactId>cache-aop</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
Configuring the caching
Select the caching implementation you require. To initialise the caching for exampple in your start up code you can add:-
- This can be done programatically as below or via annotations. For example, @CacheImplementation private CacheInterface hashMapBasedCache; @CacheImplementation(name="othercaching") private CacheInterface anotherCacheImpl;
when you assign values to the CacheInterface these will configure the actual implementation. For example in your constructor you might add,
public MyConstructor(){
super();
this.hashMapBasedCache = new HashMapBasedCache();
}
Please note if you want to autowire this with Spring then use method based autowiring e.g.
@CacheImplementation
private CacheInterface cacheInterface;
@Autowired
public setCacheImplementation(CacheInterface cacheInterface){
this.cacheInterface = cacheInterface;
}
Ehcache example
Please note caching adaptor does not initialise the ehcache cachemanager. This allows you to control the caching independently. I.e. you must initialise ehcahche in your code. Example below configures caching without using annotations. You can of course use annotations if you prefer.
private static CacheAdaptor adaptor;
if(adaptor==null){
adaptor = CacheAdaptor.getInstance();
URL configurationFileURL = getClass().getResource("ehcache.xml");
CacheManager manager = CacheManager.create(configurationFileURL);
adaptor.setCachingImplementation(new EhcacheBasedCache());
}
Please add an entry for each named cache you have into ehcache.xml or the config for your project. For example the test project for ehcahce uses.
<cache name="uk.ltd.woodsideconsultancy.test.Country.Cache"
maxElementsInMemory="5000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="10800"
timeToLiveSeconds="10800">
</cache>
For simple cache implementation
private static CacheAdaptor adaptor;
if(adaptor==null){
adaptor = CacheAdaptor.getInstance();
adaptor.setCachingImplementation(new HashMapBasedCache());
}
For simple cache memory limited implementation
private static CacheAdaptor adaptor;
if(adaptor==null){
adaptor = CacheAdaptor.getInstance();
SizedMapBasedCache cache = new SizedMapBasedCache();
cache.setMaxCaches(10);
cache.setMaxEntriesPerCache(100);
cache.setMaxDuration(3600L);
adaptor.setCachingImplementation(cache);
}
Using Multiple Caching Implementations
Please note you may have more than one caching implementation per application. e.g.
Map<String,CacheInterface> map = new HashMap<String,CacheInterface>();
map.put("othercaching", new HashMapBasedCache());
adaptor.setCachingImplementations(map);
See cache-test example for more details
Example with memcached and Spring:-
<!-- initialise cache -->
<bean id="uk.ltd.woodsideconsultancy.aop.cache.memcachedBasedCache"
class="uk.ltd.woodsideconsultancy.aop.cache.MemcachedBasedCache" />
<bean id="uk.ltd.woodsideconsultancy.aop.cache.cacheAdaptor"
class="uk.ltd.woodsideconsultancy.aop.cache.CacheAdaptor"
factory-method="getInstance">
<property name="cachingImplementation"
ref="uk.ltd.woodsideconsultancy.aop.cache.memcachedBasedCache"/>
</bean>
Keys for the cache
The cache adaptor generates keys for your cache based on the default StringKeyMaker. However you can add you own on a per cache base where for example you know that ids for an entity will suffice as a key. This can be done programatically via the methods in the CacheAdaptor which are similar to the ones used for the caching implementations or via annotations. You must implement the KeyMaker interface if you wish to do this. For example,
private static final String ANOTHER_CACHE = "myCacheName";
@KeyMakerImplementation
private KeyMaker keyMaker;
@KeyMakerImplementation(name=ANOTHER_CACHE)
private KeyMaker anotherKeyMaker;
public void setupCache(){
keyMaker = new MyKeyMaker();
anotherKeyMaker = new MyKeyMaker();
}
// or autowired for Spring @Autowired public void setKeyMaker(KeyMaker keyMaker){ this.keyMaker = keyMaker; }
Specifying Cache Keys
The @CacheKey annotation can be used to specify which method parameters should be used as cache keys. See Quick Example for details