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 ...
随机推荐
- SonarQube学习(三)- 项目代码扫描
一.前言 元旦三天假,两天半都在玩86版本DNF,不得不说,这个服真的粘度太高了,但是真的很良心. 说明: 注册账号上线100w点券,一身+15红字史诗装备以及+21强化新手武器.在线泡点一分钟888 ...
- UNraid学习随手记:显示主板、CPU传感器温度
话不多说直接开始 首先安装NerdTools 地址: https://raw.githubusercontent.com/dmacias72/unRAID-NerdPack/master/plugin ...
- Hive表的基本操作
目录 1. 创建表 2. 拷贝表 3. 查看表结构 4. 删除表 5. 修改表 5.1 表重命名 5.2 增.修.删分区 5.3 修改列信息 5.4 增加列 5.5 删除列 5.6 修改表的属性 1. ...
- vue的favicon.ico的不能修改替换问题解决。
vue的favicon.ico解决办法: 暴力替换图片: <link rel="icon" href="favicon.ico" type="i ...
- Angular入门到精通系列教程(7)- 组件(@Component)基本知识
1. 概述 2. 创建Component 组件模板 视图封装模式 特殊的选择器 :host inline-styles 3. 总结 环境: Angular CLI: 11.0.6 Angular: 1 ...
- 算法设计与分析 - 主定理Master theorem (分治法递推时间复杂度)
英文原版不上了 直接中文 定义 假设有递推关系式T(n)=aT(n/b)+f(n) 其中n为问题规模 a为递推的子问题数量 n/b为每个子问题的规模(假设每个子问题的规模基本一样) f(n)为递推以外 ...
- ACL技术(访问控制列表)
• Access Control List • 访问控制列表 • 是一种包过滤技术 • ACL基于IP包头的IP地址.四层TCP/UDP头部的端口号.[五层数据]进行过滤 • ...
- 十二:SQL注入之简要注入
SQL注入漏洞将是重点漏洞,分为数据库类型,提交方法,数据类型等方式.此类漏洞是WEB漏洞中的核心漏洞,学习如何的利用,挖掘,和修复是重要的. SQL注入的危害 SQL注入的原理 可控变量,带入数据库 ...
- nginx日志详细说明
Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(/etc/nginx/nginx.conf)中设置,两种日志都可以选择性关闭,默认都是打开的. 访问日志 访问日志主要记录 ...
- MyBatis初级实战之三:springboot集成druid
OpenWrite版: 欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kuber ...