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 配置的更多相关文章

  1. mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存

    增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查 ...

  2. Hibernate4+EhCache配置二级缓存

    本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...

  3. ehcache 配置持久化到硬盘(四)

    Ehcache默认配置的话 为了提高效率,所以有一部分缓存是在内存中,然后达到配置的内存对象总量,则才根据策略持久化到硬盘中,这里是有一个问题的,假如系统突然中断运行 那内存中的那些缓存,直接被释放掉 ...

  4. Hibernate+EhCache配置二级缓存

    步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...

  5. 转Spring+Hibernate+EHcache配置(三)

    配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cac ...

  6. 转Spring+Hibernate+EHcache配置(二)

    Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系 ...

  7. 【转】Spring+Hibernate+EHcache配置(一)

    大量数据流动是web应用性能问题常见的原因,而缓存被广泛的用于优化数据库应用.cache被设计为通过保存从数据库里load的数据来减少应用和数据库之间的数据流动.数据库访问只有当检索的数据不在cach ...

  8. Spring-boot使用Ehcache配置

    1.配置类 @Configuration @EnableCaching public class CacheConfiguration {// implements CachingConfigurer ...

  9. SSM-MyBatis-18:Mybatis中二级缓存和第三方Ehcache配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 二级缓存 Mybatis中,默认二级缓存是开启的.可以关闭. 一级缓存开启的.可以被卸载吗?不可以的.一级缓存 ...

  10. Ehcache配置详解及CacheManager使用

    <?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://w ...

随机推荐

  1. HTTP ERROR400的问题解决

    今天写添加功能,在点添加提交时报了一个"HTTP ERROR 400"的错误,如图, 请求提交的代码死活跳转不到后台,郁闷中,开启debug功能,开始一步步排查, 1.先单独把跳转 ...

  2. Turtlebot3新手教程:仿真

    本文章针对如何利用turtlebot3实现仿真功能进行讲解 测试环境:Ubuntu 16.04 和 ROS Kinetic Kame. 注意:TurtleBot3 Simulation 依赖 turt ...

  3. Linux系统下qt程序运行找不到IGL

    linux下使用QT5运行时出现两个问题: cannot find -lGL collect2:error:ld returned 1 exit status 这是因为系统缺少链接库,执行两条命令即可 ...

  4. Java并发包源码学习系列:CLH同步队列及同步资源获取与释放

    目录 本篇学习目标 CLH队列的结构 资源获取 入队Node addWaiter(Node mode) 不断尝试Node enq(final Node node) boolean acquireQue ...

  5. 【原创】Linux PCI驱动框架分析(三)

    背 景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本 ...

  6. Debian安装HomeBrew

    前言 HomeBrew 的用处我想使用 Mac 的开发人员都知道, 本篇讲解如何在 Debian 上安装 BrewLinux 更新: 后来发现并不是很好用, 不建议使用 官方推荐的脚本安装 注意这里只 ...

  7. 【JAVA并发第三篇】线程间通信

    线程间的通信 JVM在运行时会将自己管理的内存区域,划分为不同的数据区,称为运行时数据区.每个线程都有自己私有的内存空间,如下图示: Java线程按照自己虚拟机栈中的方法代码一步一步的执行下去,在这一 ...

  8. RocketMq消息 demo

    参考 https://blog.csdn.net/asdf08442a/article/details/54882769 整理出来的测试 demo 1.produce 生产者 1 package co ...

  9. Docker 如何动态给SpringBoot项目传参

    关于SpringBoot配置数据源 在项目开发中,我们往往需要配置多套不同的配置环境例如:本地开发.测试环境.部署环境.每一个环境的数据源配置可能都不同,因此需要写不同的数据源配置.如果用Docker ...

  10. Android事件分发机制三:事件分发工作流程

    前言 很高兴遇见你~ 本文是事件分发系列的第三篇. 在前两篇文章中,Android事件分发机制一:事件是如何到达activity的? 分析了事件分发的真正起点:viewRootImpl,Activit ...