<aop:config> <!-- 事务切入点通知 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))" order="2"/> <!-- 缓存切入点通知 --> <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* *..*Service.*(..))" order="0"/> </aop:config>
<!-- 缓存管理器 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactoryBean" /> </bean> <!-- 缓存管理器工厂bean --> <bean id="cacheManagerFactoryBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> <!-- 自定义缓存key生成器 --> <bean id="surveyparkKeyGenerator" class="com.surveypark.cache.SurveyparkKeyGenerator" /> <!-- 缓存通知 --> <cache:advice id="cacheAdvice" cache-manager="cacheManager" key-generator="surveyparkKeyGenerator"> <cache:caching cache="surveyparkCache"> <cache:cacheable method="get*" /> <cache:cacheable method="load*" /> <cache:cacheable method="find*" /> <cache:cache-evict method="save*" all-entries="true" /> <cache:cache-evict method="update*" all-entries="true"/> <cache:cache-evict method="delete*" all-entries="true"/> <cache:cache-evict method="clear*" all-entries="true"/> <cache:cache-evict method="toggle*" all-entries="true"/> <cache:cache-evict method="move*" all-entries="true"/> <cache:cache-evict method="batch*" all-entries="true"/> <cache:cache-evict method="execute*" all-entries="true"/> </cache:caching> </cache:advice>
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="surveyparkCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>
public class SurveyparkKeyGenerator implements KeyGenerator{ public Object generate(Object arg0, Method arg1, Object... arg2) { String className = arg0.getClass().getSimpleName(); String mname = arg1.getName(); String params = StringUtil.arr2Str(arg2); String key = className + "@" + arg0.hashCode() + "." + mname + "("+params+")" ; return key; } }
StringUtil.java
public static String arr2Str(Object[] arr) { String temp = "" ; if(ValidateUtil.isValid(arr)){ for(Object s : arr){ temp = temp + s + "," ; } return temp.substring(0,temp.length() - 1); } return temp; }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!