ehCache 配置
package com.jy.modules.cms; import java.io.Serializable; import net.sf.ehcache.Cache;
import net.sf.ehcache.Element; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
//编写自定义拦截器
public class MethodCacheInterceptor
implements MethodInterceptor, InitializingBean
{
private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class);
private Cache cache; public void setCache(Cache cache)
{
this.cache = cache;
} public Object invoke(MethodInvocation invocation)
throws Throwable
{
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments(); logger.debug("Find object from cache is " + this.cache.getName()); String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = this.cache.get(cacheKey); if (element == null) {
logger.debug("Hold up method , Get method result and create cache........!");
Object result = invocation.proceed();
element = new Element(cacheKey, (Serializable)result);
this.cache.put(element);
}
return element.getValue();
} 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();
} public void afterPropertiesSet()
throws Exception
{
Assert.notNull(this.cache, "Need a cache. Please use setCache(Cache) create it.");
}
}
<!-- 采用ehCache的工厂进行缓存配置 start -->
<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="ehcacheManager"/>
<property name="cacheName">
<value>SYS_DATE_CACHE</value>
</property>
</bean> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean> <!-- find/create cache拦截器 -->
<bean id="methodCacheInterceptor" class="com.jy.modules.cms.MethodCacheInterceptor">
<property name="cache" ref="ehCache" />
</bean>
<!-- flush cache拦截器 -->
<bean id="methodCacheAfterAdvice" class="com.jy.platform.core.ehcache.MethodCacheAfterAdvice">
<property name="cache" ref="ehCache" />
</bean> <!-- 采用ehCache的工厂进行缓存配置 end --> <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="methodCacheInterceptor"/>
<property name="patterns">
<list>
<value>com.jy.modules.platform.sysorg.service.*query.*</value>
<value>com.jy.modules.platform.sysorg.service.*find.*</value>
<value>com.jy.modules.platform.sysorg.service.*search.*</value>
<value>com.jy.modules.platform.sysconfig.service.*query.*</value>
<value>com.jy.modules.platform.sysconfig.service.*find.*</value>
<value>com.jy.modules.platform.sysconfig.service.*search.*</value>
<value>com.jy.modules.platform.sysdict.service.*query.*</value>
<value>com.jy.modules.platform.sysdict.service.*find.*</value>
<value>com.jy.modules.platform.sysdict.service.*search.*</value>
</list>
</property>
</bean>
<bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="methodCacheAfterAdvice"/>
<property name="patterns">
<list>
<value>com.jy.modules.platform.sysorg.service.*update.*</value>
<value>com.jy.modules.platform.sysorg.service.*delete.*</value>
<value>com.jy.modules.platform.sysorg.service.*insert.*</value>
<value>com.jy.modules.platform.sysconfig.service.*update.*</value>
<value>com.jy.modules.platform.sysconfig.service.*delete.*</value>
<value>com.jy.modules.platform.sysconfig.service.*insert.*</value>
<value>com.jy.modules.platform.sysdict.service.*update.*</value>
<value>com.jy.modules.platform.sysdict.service.*delete.*</value>
<value>com.jy.modules.platform.sysdict.service.*insert.*</value>
</list>
</property>
</bean>
ehCache 配置的更多相关文章
- mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存
增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...
- Hibernate4+EhCache配置二级缓存
本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...
- ehcache 配置持久化到硬盘(四)
Ehcache默认配置的话 为了提高效率,所以有一部分缓存是在内存中,然后达到配置的内存对象总量,则才根据策略持久化到硬盘中,这里是有一个问题的,假如系统突然中断运行 那内存中的那些缓存,直接被释放掉 ...
- Hibernate+EhCache配置二级缓存
步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...
- 转Spring+Hibernate+EHcache配置(三)
配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cac ...
- 转Spring+Hibernate+EHcache配置(二)
Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系 ...
- 【转】Spring+Hibernate+EHcache配置(一)
大量数据流动是web应用性能问题常见的原因,而缓存被广泛的用于优化数据库应用.cache被设计为通过保存从数据库里load的数据来减少应用和数据库之间的数据流动.数据库访问只有当检索的数据不在cach ...
- Spring-boot使用Ehcache配置
1.配置类 @Configuration @EnableCaching public class CacheConfiguration {// implements CachingConfigurer ...
- SSM-MyBatis-18:Mybatis中二级缓存和第三方Ehcache配置
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 二级缓存 Mybatis中,默认二级缓存是开启的.可以关闭. 一级缓存开启的.可以被卸载吗?不可以的.一级缓存 ...
- Ehcache配置详解及CacheManager使用
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://w ...
随机推荐
- 对CROS OPTIONS预检请求的一些思考
前后端分离模大势所趋,跨域问题更是老生常谈. 问题背景: 浏览器最基本的安全规范-同源策略.所谓同源是指域名.协议.端口相同.不同源的浏览器脚本(javascript.ActionScript.can ...
- C#中的格式
格式模式 说明 : d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义. ddd ...
- 史上最全的pycharm快捷键!快进来点个收藏吧
Alt+Enter 自动添加包 Ctrl+t SVN更新 Ctrl+k SVN提交 [编辑类:](https://link.zhihu.com/?target=https://www.jianshu. ...
- vue的路由组件挂载。
vue通过多种方式可以将组件挂载到一个页面上.挂载方式有四种.其实也并不止四种.这里呢就简单的提四种方式去怎样挂载组件. 第一种就是作为标签形式挂载.前面也提到. 后面的就是一般的挂载组件和按需挂载组 ...
- 如何优雅的传递 stl 容器作为函数参数来实现元素插入和遍历?
问题背景 开始正文之前,做一些背景铺垫,方便读者了解我的工程需求.我的项目是一个客户端消息分发中心,在连接上消息后台后,后台会不定时的给我推送一些消息,我再将它们转发给本机的其它桌面产品去做显示.后台 ...
- 百度智能(文本识别),API传图OC代码与SDK使用
百度智能中的文本识别中的身份证识别,有API方式和SDK方式 API方式 百度智能(文本识别),百度API传图没有提供OC的示例,这里提供一下 - (void)OCTest:(NSString*)to ...
- node中 path.join 和 path.resovle 区别
console.log(__dirname) console.log('----') console.log(path.resolve(__dirname, '/a/b', '../')) conso ...
- mybatis入门教程之搭建一个简单的mybatis项目并启动它
一.准备条件: 1.依赖jar包:mybatis核心包(必须).lombok插件包(非必须)以及MySQL数据库连接驱动包(必须) <dependency> <groupId> ...
- Linux TCP漏洞 CVE-2019-11477 CentOS7 修复方法
CVE-2019-11477漏洞简单介绍 https://cert.360.cn/warning/detail?id=27d0c6b825c75d8486c446556b9c9b68 RedHat用户 ...
- [USACO13DEC]牛奶调度Milk Scheduling
原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=4096 容易想到的一个测略就是,优先考虑结束时间小的牛.所以我们对所有牛按照结束时间排序.然 ...