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. 30天自制操作系统-day1

    30天自制操作系统(linux环境)--第一天 我是在CentOS的环境上面实现的,使用ubuntu的环境也是类似的 第一步:因为要对二进制文件进行编辑,所以安装二进制编辑器hexedit(当然其他的 ...

  2. java中装箱,拆箱

    (1)包装类 java中不能定义基本数据类型的对象,但是提供了基本数据类型的包装类来解决这一问题,例如int类型的包装类Integer, 包装类的常用方法有Integer.ParseInt()方法类将 ...

  3. 解析SwiftUI布局细节(二)循环轮播+复杂布局

    前言 上一篇我们总结的主要是VStack里面的东西,由他延伸到 @ViewBuilder, 接着我们上一篇总结的我们这篇内容主要说的是下面的几点,在这些东西说完后我准备解析一下苹果在SiwftUI文档 ...

  4. 解决 unknown filesystem type ntfs U盘/移动硬盘挂载出错问题

    大内存U盘或者移动硬盘挂在再Linux 时,报错unknown filesystem type ntfs 1.安装ntfs-3g wget http://tuxera.com/opensource/n ...

  5. 【Oracle】生成随机数

    Oracle生成随机数: dbms_random.string(opt, 6)     --括号里的opt要从下面的列表中选择,数字代表要生成几位随机数,如果是1位的话,就改成1 以此类推 opt可取 ...

  6. springmvc 字符串转日期格式

    http://www.mamicode.com/info-detail-2485490.html

  7. Docker下梦织CMS的部署

    摘要:Docker的广泛应用相对于传统的虚拟机而言提高了资源的利用率,推广后docker的影响不容忽视,在启动速度.硬盘.内存.运行密度.性能.隔离性和迁移性方面都有很大的提高.本次实训我们在cent ...

  8. JAVA获取当前文件路径this.getClass().getResource方法详细讲解

    public class Test { public void run() { // TODO Auto-generated method stub System.out.println(" ...

  9. celery应用

    celery---分布式任务队列 Celery是一个简单,灵活且可靠的分布式系统,可以处理大量消息,同时为操作提供维护该系统所需的工具. Celery是一个基于python开发的模块,可以帮助我们对任 ...

  10. JVM(三)从JVM源码角度看类加载器层级关系和双亲委派

    类加载器我们都知道有如下的继承结构,这个关系只是逻辑上的父子关系. 我们一直听说引导类加载器没有实体,为什么没有实体呢? 因为引导类加载器只是一段C++代码并不是什么实体类,所谓的引导类加载器就是那一 ...