使用EhCache和Spring AOP实现计算结果缓存
原文:http://kim-miao.iteye.com/blog/1622686
一.Ehcache简介
EhCache是一个纯Java的进程内缓存框架,具有如下特点:
1. 快速简单,非常容易和应用集成。
2.支持多种缓存策略 。
3. 缓存数据有两级:内存和磁盘,因此无需担心容量问题 。
4. 缓存数据会在虚拟机重启的过程中写入磁盘 。
5. 可以通过RMI、可插入API等方式进行分布式缓存。
6. 具有缓存和缓存管理器的侦听接口 。
7. 支持多缓存管理器实例,以及一个实例的多个缓存区域 等特点。
二.Ehcache配置的相关参数
Ehcache的配置很灵活,官方提供的配置方式有好几种,你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。下面以最常用的XML配置为例说下配置的相关参数的意义,ehcache.xml是最常见的一个文件,ehcache一般会通过CacheManager从classpath加载该文件完成Cache的实例化。
1.ehcache.xml中的配置信息
ehcache.xml片段:
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache name="name" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" overflowToDisk="false"> </defaultCache> <cache name="levelOneCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="1000" overflowToDisk="false" /> </ehcache>
<bean id="cacheManager" /> </property> <property name="cacheName"> <value>configCache</value> </property> </bean>
package org.mango.cache.ehcache; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class EhcacheTest { public static void main(String[] args) { Resource res = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(res); CacheManager cacheManager = (CacheManager) factory.getBean("cacheManager"); Cache levelOneCache = cacheManager.getCache("levelOneCache"); CacheObject cacheObject = null; for (int i = 0; i < 10; i++) { Element element = levelOneCache.get("key"); if (element == null) { cacheObject = new CacheObject("test"); element = new Element("key", cacheObject); levelOneCache.put(element); System.out.println("cacheObject[" + cacheObject + "]" + ",无法从缓存中取到"); } else { cacheObject = (CacheObject) element.getValue(); System.out.println("cacheObject[" + cacheObject + "]" + ",从缓存中取到"); } } } }
cacheObject[name:test],无法从缓存中取到 cacheObject[name:test],从缓存中取到 cacheObject[name:test],从缓存中取到 cacheObject[name:test],从缓存中取到 cacheObject[name:test],从缓存中取到
public interface TestService { /** * 根据userId取得用户名。 * * @param userId * @return */ public String getUserName(String userId); }
public class TestServiceImpl implements TestService { /* * @see org.mango.cache.ehcache.TestService#getUserName(java.lang.String) */ public String getUserName(String userId) { return userId; } }
public class CacheInterceptor implements MethodInterceptor { private Cache cache; /** * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) */ public Object invoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); String methodName = method.getName(); Object[] arguments = invocation.getArguments(); Object result = invocation.proceed(); String targetName = method.getDeclaringClass().getName(); String key = getCacheKey(targetName, methodName, arguments); Element element = cache.get(key); if (element == null) { result = invocation.proceed(); System.out.println("第一次调用方法并缓存其值:" + result); cache.put(new Element(key, result)); } else { result = element.getValue(); System.out.println("从缓存中取得的值为:" + result); } return result; } /** * 生成缓存中的KEY值。 */ protected String getCacheKey(String targetName, String methodName, Object[] arguments) { StringBuffer sb = new StringBuffer(); sb.append(targetName).append(".").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i = 0; i < arguments.length; i++) { sb.append(".").append(arguments[i]); } } return sb.toString(); } public void setCache(Cache cache) { this.cache = cache; } }
<bean id="testService" /> <bean id="serviceMethodInterceptor" /> </property> </bean> <bean id="serviceAutoProxyCreator" name="code">public class ServiceTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); TestService testService = (TestService) context.getBean("testService"); for (int i = 0; i < 5; i++) { testService.getUserName("mango"); } } }
第一次调用方法并缓存其值:mango从缓存中取得的值为:mango从缓存中取得的值为:mango从缓存中取得的值为:mango从缓存中取得的值为:mango