spring ehcache 缓存管理

 

 

 

 

 

<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捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值