这是接上篇<<Ehcache入门>>的进阶篇。
在实际的项目开发中,肯定会根据具体业务,数据大小,复杂度采用不同的技术实现方式,Ehcache在实际项目开发中一般被用来缓存方法结果集,且可以与Spring无缝集成,完全交由Spring——Aop拦截器来完成,我们只需处理好业务数据获取环节。
1.ehcache.xml配置:
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <cache name="gov.csc.ems.cache.METHOD_CACHE" maxElementsInMemory="300" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>
2. Spring集成Ehcache配置,cacheManage,methodCache
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> </bean> <bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>gov.csc.ems.cache.METHOD_CACHE</value> </property> </bean>
3. 完成上面的基础配置,Spring是靠拦截器来缓存我们的方法,因此建立我们自己的方法拦截器MethodCacheInterceptor。MethodCacheInterceptor实现了org.aopalliance.intercept.MethodInterceptor接口。
import java.io.Serializable; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean { private Cache cache; /** * sets cache name to be used */ public void setCache(Cache cache) { this.cache = cache; } /** * Checks if required attributes are provided. */ public void afterPropertiesSet() throws Exception { Assert.notNull(cache, "A cache is required. Use setCache(Cache) to provide one."); } /** * main method caches method result if method is configured for caching * method results must be serializable */ public Object invoke(MethodInvocation invocation) throws Throwable { String targetName = invocation.getThis().getClass().getName(); String methodName = invocation.getMethod().getName(); Object[] arguments = invocation.getArguments(); Object result; String cacheKey = getCacheKey(targetName, methodName, arguments); Element element = cache.get(cacheKey); if (element == null) { result = invocation.proceed(); element = new Element(cacheKey, (Serializable) result); cache.put(element); } return element.getValue(); } /** * creates cache key: targetName.methodName.argument0.argument1... */ private 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(); } }
invoke方法中:
String cacheKey = getCacheKey(targetName, methodName, arguments); Element element = cache.get(cacheKey); if (element == null) { result = invocation.proceed(); element = new Element(cacheKey, (Serializable) result); cache.put(element); } return element.getValue();
这段代码就是用来缓存我们的方法结果,拦截器先用key(key=className + methodName + arguments)查询缓存,缓存中存在则返回,否则调用invocation.proceed(),根据我们自己的实现查询数据,即第一次查询,以后每次就走缓存。
4.Spring中定义拦截器配置:
<bean id="methodCacheInterceptor" class="gov.csc.ems.util.cache.interceptor.MethodCacheInterceptor"> <property name="cache"> <ref local="methodCache" /> </property> </bean>
5.Aop正则表达式的切入点配置:
<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="methodCacheInterceptor" /> </property> <property name="patterns"> <list> <value>.*</value> </list> </property> </bean>
patterns要拦截的规则,可以拦截指定的方法,如.getOrg*,.getUser*,这里我拦截所有的方法。
6.交给Spring代理
<bean id="codeCacheBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <bean class="gov.csc.ems.util.cache.CodeCache" /> </property> <property name="interceptorNames"> <list> <value>methodCachePointCut</value> </list> </property> </bean>
其中<bean class="gov.csc.ems.util.cache.CodeCache" />它就是我们缓存的类,缓存就是它里面的所有方法,当我们要用到它里面的方法时,如果此方法配置了缓存拦截,就会先走拦截器。
7.要缓存的方法类CodeCache
public class CodeCache { public CodeCache() { super(); } public Map getOrgCache() { //从数据库查询org ...... } public Map getUserCache() { //从数据库查询user ...... } }
8.测试
我这里写了个servlet,根据Spring获取到CodeCache,
CodeCache codeCache=(CodeCache) BeanUtil.getBean("codeCacheBean"); Map codeBasOrgs=codeCache.getOrgCache(); System.out.println(codeBasOrgs.get("0001"));
分别在拦截器和CodeCache中打上断点,运行发现每次先走拦截器,而且第一次会走CodeCache查询,以后就直接取缓存了。
9.缓存的更新
如新添加了Org或user,CodeCache.getOrgCache.put(key,value)即可添加到缓存。
相关推荐
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
使用springboot 集成ehcache,里面附带数据库建库脚本,controller,service以及对ehcache集成,操作的详细样例,使用spring注解形式。
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
在Spring+Hibernate集成环境中使用EhCache缓存做的开发测试,在线程中打开数据库查询数据,及关闭数据库后查询数据的结果.
【EhCache】Java缓存框架使用EhCache结合Spring AOP EhCache是一个纯Java的进程内缓存框架,具有如下特点: 1. 快速简单,非常容易和应用集成。 2.支持多种缓存策略 。 3. 缓存数据有两级:内存和磁盘,因此无需...
struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建
本篇文章主要介绍了详解Spring MVC 集成EHCache缓存,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2294952
项目简介:此项目只是简单的集成spring+springmvc+shiro+ehcahce 二: 步骤说明: 1:项目集成spring 在web.xml中配置spring容器的监听器。 2:项目集成springmvc 在web.xml中配置前端控制器 3:项目集成...
在本篇文章里小编给大家整理的是关于Spring Boot集成Ehcache缓存解决方式,需要的朋友们可以学习下。
Spring Boot JPA 二级缓存(ehcache) Spring Boot Hikari 连接池详解 Spring Boot Caffeine 本地缓存 Spring Boot Redis 缓存 Spring Boot Redis 分布式锁 Spring Boot Event 事件发布与监听 Spring Boot 集成 ...
在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存是个不错的选择,因此本文将介绍如何在Spring Boot的缓存支持中使用Redis进行数据缓存...
SpringBoo2.x整合Ehcache3.x作为缓存底层存储。
- Spring 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - spring-docs - Spring 文档生成工具,包括 Swagger - spring-bussiness - Spring 业务应用,包括 AOP、过滤...
spring-boot是一个快速的集成框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。这篇文章主要介绍了spring-boot整合ehcache实现缓存机制,需要的朋友可以参考下
springboot1.x基于spring注解实现J2Cache两级缓存集成,一级缓存:ehcache,二级缓存:redis,代码可直接运行(根据项目中的README.md提示)
本篇文章主要介绍了详解spring boot集成ehcache 2.x 用于hibernate二级缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ehcache的功能的实现,和Spring框架的集成,两个项目
NULL 博文链接:https://hanqunfeng.iteye.com/blog/603719