Declarative Caching using Spring, Ehcache and Java 5 Annotations
Submitted by rowell on Mon, 08/04/2008 - 22:41
Java Class
package services.interfaces;
import org.springmodules.cache.annotations.Cacheable;
import services.model.Location;
public interface LocationService {
@Cacheable(modelId="getLocationCacheModel")
public Location getLocation(String zipcode);
}
import org.springmodules.cache.annotations.Cacheable;
import services.model.Location;
public interface LocationService {
@Cacheable(modelId="getLocationCacheModel")
public Location getLocation(String zipcode);
}
ehcache.xml
<ehcache>
<defaultCache
maxElementsInMemory="500"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
<cache name="getLocationCache"
maxElementsInMemory="50"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
<defaultCache
maxElementsInMemory="500"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
<cache name="getLocationCache"
maxElementsInMemory="50"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
Spring Configuration File
Add the following namespace definition:
http://www.springmodules.org/schema/ehcache
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
<ehcache:config configLocation="classpath:ehcache.xml" />
<ehcache:annotations>
<ehcache:caching id="getLocationCacheModel"
cacheName="getLocationCache" />
</ehcache:annotations>
<ehcache:annotations>
<ehcache:caching id="getLocationCacheModel"
cacheName="getLocationCache" />
</ehcache:annotations>