spring ehcache 缓存管理

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



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

 

 

目录
相关文章
|
3天前
|
缓存 NoSQL Java
在 Spring Boot 应用中使用 Spring Cache 和 Redis 实现数据查询的缓存功能
在 Spring Boot 应用中使用 Spring Cache 和 Redis 实现数据查询的缓存功能
12 0
|
5天前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
23 2
|
6天前
|
缓存 监控 负载均衡
Java一分钟之-Ehcache:分布式缓存系统
【6月更文挑战第17天】**Ehcache是Java的开源缓存库,支持本地和分布式缓存,提供负载均衡、数据复制和容错能力。常见问题包括网络分区导致的数据不一致、缓存雪崩和配置不当引起的性能瓶颈。解决策略涉及选择强一致性策略、设置合理缓存过期时间和监控调整配置。使用Ehcache需添加相关依赖,并配置分布式缓存,如示例所示,通过CacheManager创建和管理缓存。实践中,持续监控和优化配置至关重要。**
25 1
|
10天前
|
缓存 Java
修改缓存供应商--EhCache
修改缓存供应商--EhCache
|
14天前
|
监控 Java UED
Java一分钟之-Spring Cloud Netflix Hystrix:容错管理
【6月更文挑战第9天】Spring Cloud Hystrix是用于微服务容错管理的库,通过断路器模式防止服务雪崩。本文介绍了Hystrix的基本概念,如断路器、线程隔离和fallback机制,并展示了如何快速上手,包括添加依赖、启用注解和编写Hystrix命令。此外,还讨论了常见问题(如断路器打开、资源泄漏和不当的Fallback策略)及其解决方案。通过自定义Hystrix指标监控,可以进一步优化系统性能。理解Hystrix工作原理并适时调整配置,对于构建健壮的微服务至关重要。
123 3
|
2天前
|
JavaScript Java 数据库连接
【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理
【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理
7 0
|
30天前
|
缓存 NoSQL Java
Spring Cache之本地缓存注解@Cacheable,@CachePut,@CacheEvict使用
SpringCache不支持灵活的缓存时间和集群,适合数据量小的单机服务或对一致性要求不高的场景。`@EnableCaching`启用缓存。`@Cacheable`用于缓存方法返回值,`value`指定缓存名称,`key`定义缓存键,可按SpEL编写,`unless`决定是否不缓存空值。当在类上使用时,类内所有方法都支持缓存。`@CachePut`每次执行方法后都会更新缓存,而`@CacheEvict`用于清除缓存,支持按键清除或全部清除。Spring Cache结合Redis可支持集群环境。
95 6
|
5天前
|
XML Java 数据格式
Spring框架第三章(基于注解管理bean)
Spring框架第三章(基于注解管理bean)
|
5天前
|
XML Java 数据格式
Spring框架第二章(基于XML管理bean)
Spring框架第二章(基于XML管理bean)
|
13天前
|
缓存 NoSQL Java
Java一分钟之-Spring Data Redis:使用Redis做缓存
【6月更文挑战第10天】Spring Data Redis是Spring框架的一部分,简化了Java应用与Redis的集成,支持多种数据结构操作。本文介绍了其基本使用,包括添加依赖、配置Redis连接及使用RedisTemplate。还讨论了常见问题,如序列化、缓存穿透和雪崩,并提供解决方案。通过实战示例展示了缓存与数据库读写分离的实现,强调了Spring Data Redis在提升系统性能中的作用。
41 0