user.home–用户主目录
user.dir–用户当前工作目录
java.io.tmpdir–默认临时文件路径,就是在tomcat的temp目录-->
<diskStore path="java.io.tmpdir"/>eternal-设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超
时限制且元素永不消亡。
timeToIdleSeconds-设置某个元素消亡前的停顿时间。
也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。
这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则
设置该属性也无用)。
如果该值是0就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds-为元素设置消亡前的生存时间。
也就是一个元素从构建到消亡的最大时间间隔值。
这只能在元素不是永久驻留时有效。
overflowToDisk-设置当内存中缓存达到maxInMemory限制时元素是否可写到磁盘
上。
-->
<!--timeToLiveSeconds的时间一定要大于等于timeToIdleSeconds的时间按-->
<cachename="DEFAULT_CACHE"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="500"
timeToLiveSeconds="500"
overflowToDisk="true"
/>
</ehcache>InitializingBean{
privatestaticfinalLoglogger=LogFactory
.getLog(MethodCacheInterceptor.class);this.cache=cache;
}super();
}*拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,否则,返回数据库查询结果,并将查询结果放入cache
*/
publicObjectinvoke(MethodInvocationinvocation)throwsThrowable{
StringtargetName=invocation.getThis().getClass().getName();
StringmethodName=invocation.getMethod().getName();
Object[]arguments=invocation.getArguments();
Objectresult;
logger.debug("Findobjectfromcacheis"+cache.getName());
StringcacheKey=getCacheKey(targetName,methodName,arguments);
Elementelement=cache.get(cacheKey);
longstartTime=System.currentTimeMillis();
if(element==null){
logger
.debug("Holdupmethod,Getmethodresultandcreatecache........!");
result=invocation.proceed();
element=newElement(cacheKey,(Serializable)result);
cache.put(element);
longendTime=System.currentTimeMillis();
logger.info(targetName+"."+methodName+"方法被首次调用并被缓存。耗时"
+(endTime-startTime)+"毫秒"+"cacheKey:"
+element.getKey());
}else{
longendTime=System.currentTimeMillis();
logger.info(targetName+"."+methodName+"结果从缓存中直接调用。耗时"
+(endTime-startTime)+"毫秒"+"cacheKey:"
+element.getKey());
}
returnelement.getValue();
}*获得cachekey的方法,cachekey是Cache中一个Element的唯一标识cachekey包括包名+类名+方法名+参数
*/
privateStringgetCacheKey(StringtargetName,StringmethodName,
Object[]arguments){
StringBuffersb=newStringBuffer();
sb.append(targetName).append(".").append(methodName);
if((arguments!=null)&&(arguments.length!=0)){
for(inti=0;i<arguments.length;i++){
sb.append(".").append(arguments[i]);
}
}
returnsb.toString();
}*implementInitializingBean,检查cache是否为空
*/
publicvoidafterPropertiesSet()throwsException{
Assert.notNull(cache,
"Needacache.PleaseusesetCache(Cache)createit.");
}publicclassMethodCacheAfterAdviceimplementsAfterReturningAdvice,
InitializingBean{
privatestaticfinalLoglogger=LogFactory
.getLog(MethodCacheAfterAdvice.class);this.cache=cache;
}super();
}Objectarg3)throwsThrowable{
StringclassName=arg3.getClass().getName();
Listlist=cache.getKeys();
for(inti=0;i<list.size();i++){
StringcacheKey=String.valueOf(list.get(i));
if(cacheKey.startsWith(className)){
cache.remove(cacheKey);
logger.debug("removecache"+cacheKey);
}
}
}Assert.notNull(cache,
"Needacache.PleaseusesetCache(Cache)createit.");
}
}
这个方法主要是保证缓存的同步,保持与数据库的数据一致性。
第三步:配置Bean了,applicationContext-ehcache.xml文件就是Spring中的Ioc(控制反转容器)的描述了。上面的只是简单的写了两个方法,具体的能起到什么作用,以及何时起作用,以及怎样用声明式的方式(AOP)和Bean结合。<beansxmlns="[url]http://www.springframework.org/schema/beans[/url]"
xmlns:xsi="[url]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]"><beanid="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<propertyname="transactionManager">
<refbean="transactionManager"/>
</property>
<!--配置事务属性-->
<propertyname="transactionAttributes">
<props>
<propkey="delete*">PROPAGATION_REQUIRED</prop>
<propkey="update*">PROPAGATION_REQUIRED</prop>
<propkey="save*">PROPAGATION_REQUIRED</prop>
<propkey="find*">PROPAGATION_REQUIRED,readOnly</prop>
<propkey="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!--引用ehCache的配置-->
<beanid="defaultCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<propertyname="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean><beanid="ehCache"
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<propertyname="cacheManager">
<reflocal="defaultCacheManager"/>
</property>
<propertyname="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean><beanid="methodCacheInterceptor"
class="com.w3cs.cache.ehcache.MethodCacheInterceptor">
<propertyname="cache">
<reflocal="ehCache"/>
</property>
</bean>
<!--flushcache拦截器-->
<beanid="methodCacheAfterAdvice"
class="com.w3cs.cache.ehcache.MethodCacheAfterAdvice">
<propertyname="cache">
<reflocal="ehCache"/>
</property>
</bean>class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<propertyname="advice">
<reflocal="methodCacheInterceptor"/>
</property>
<propertyname="patterns">
<list>
<value>.*find.*</value>
<value>.*get.*</value>
</list>
</property>
</bean>
<beanid="methodCachePointCutAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<propertyname="advice">
<reflocal="methodCacheAfterAdvice"/>
</property>
<propertyname="patterns">
<list>
<value>.*create.*</value>
<value>.*update.*</value>
<value>.*delete.*</value>
</list>
</property>
</bean><beanid="autoproxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!--可以是Service或DAO层(最好是针对业务层*Service)-->
<propertyname="beanNames">
<list>
<value>*DAO</value>
</list>
</property>
<propertyname="interceptorNames">
<list>
<value>methodCachePointCut</value>
<value>methodCachePointCutAdvice</value>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>