@Cacheable注解式缓存不起作用的情形
@Cacheable注解式缓存使用的要点:正确的注解式缓存配置,注解对象为spring管理的hean,调用者为另一个对象。有些情形下注解式缓存是不起作用的:同一个bean内部方法调用,子类调用父类中有缓存注解的方法等。后者不起作用是因为缓存切面必须走代理才有效,这时可以手动使用CacheManager来获得缓存效果。
使用注解式缓存的正确方式:
<cache:annotation-driven cache-manager="springCacheManager" proxy-target-class="false"/>
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="cacheManagerName" value="ehcache"/>
</bean>
<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean> 要点:@Cacheable(value="必须使用ehcache.xml已经定义好的缓存名称,否则会抛异常")
@Component
public class CacheBean {
@Cacheable(value="passwordRetryCache",key="#key")
public String map(String key) {
System.out.println("get value for key: "+key);
return "value: "+key;
}
public String map2(String key) {
return map(key);
}
} @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:cache.xml" })
public class CacheTester {
@Autowired CacheManager cacheManager;
@Autowired CacheBean cacheBean;
@Test public void cacheManager() {
System.out.println(cacheManager);
}
@Test public void cacheAnnotation() {
cacheBean.map("a");
cacheBean.map("a");
cacheBean.map("a");
cacheBean.map("a");
System.out.println(cacheManager.getCacheNames());
}
}
输出:
get value for key: a
[authorizationCache, authenticationCache, shiro-activeSessionCache, passwordRetryCache]
稍微改造一下,让ehcache支持根据默认配置自动添加缓存空间,这里提供自定义的MyEhCacheCacheManager即可
<bean id="springCacheManager" class="com.itecheast.ite.domain.util.MyEhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean>
另一种改造方式,找不到已定义的缓存空间时不缓存,或者关闭全部缓存。把cacheManagers配置去掉就可以关闭圈闭缓存。
<bean id="springCacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<list>
<bean class="org.springframework.cache.ehcache.EhCacheCacheManager"></bean>
<!-- <bean class="com.itecheast.ite.domain.util.MyEhCacheCacheManager"></bean> 这个会自动创建缓存空间 -->
</list>
</property>
<property name="fallbackToNoOpCache" value="true"/>
</bean>
调用相同类或父类方法没有缓存效果:这时可以选择手动使用CacheManager。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:cache.xml" })
public class CacheTester {
@Test public void cacheAnnotation() {
this.map("a");
this.map("a");
}
@Cacheable(value="passwordRetryCache",key="#key")
public String map(String key) {
System.out.println("get value for key: "+key);
return "value: "+key;
}
}
或者再换一种方式:手动使用代理方式调用同类方法也是可以的
public class CacheBean {
@Autowired ApplicationContext applicationContext;
@Cacheable(value="passwordRetryCache",key="#key")
public String map(String key) { //方法不能为private,否则也没有缓存效果
System.out.println("get value for key: "+key);
return "value: "+key;
}
public String map2(String key) {
CacheBean proxy = applicationContext.getBean(CacheBean.class);
return proxy.map(key); //这里使用proxy调用map就可以缓存,而直接调用map则没有缓存
}
}
本文转自《https://www.xlongwei.com/detail/-cacheable%E6%B3%A8%E8%A7%A3%E5%BC%8F%E7%BC%93%E5%AD%98%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%E7%9A%84%E6%83%85%E5%BD%A2》
@Cacheable注解式缓存不起作用的情形的更多相关文章
- Spring Cacheable 注解不缓存null值
用Cacheable注解时,发现空值,也会被缓存下来.如果我们期望空值不被缓存,可以做如下设置: @Cacheable(key = "#id", unless="#res ...
- SpringBoot + redis + @Cacheable注解实现缓存清除缓存
一.Application启动类添加注解 @EnableCaching 二.注入配置 @Bean public CacheManager cacheManager(RedisTemplate redi ...
- @Cacheable注解在spring3中的使用-实现缓存
转: http://blog.csdn.net/chenleixing/article/details/44815443 在软件开发中使用缓存已经有一个非常久的历史了.缓存是一种很好的设计思想,一旦 ...
- 小白的springboot之路(八)、继承Redis以及@Cacheable注解实现Redis缓存
0.前言 在项目中,缓存作为一种高效的提升性能的手段,几乎必不可少,Redis作为其中的佼佼者被广泛应用: 一.spring boot集成Redis 1.添加依赖 <dependency> ...
- 注解式项目开发!详细解析Java中各个注解的作用和使用方式
@Target 作用: 指明了修饰的这个注解的使用范围, 即被描述的注解可以用在哪里 @Target(ElementType.Type) ElementType取值的类型: TYPE: 类,接口或者枚 ...
- spring mvc + ehcache 利用注解实现缓存功能
我的spring是3.1的,因为项目需求,需要在查询时候加上缓存,小白一个,完全没有用过缓存(ehcache),摸索了一天终于会了一点通过注解来使用ehcache进行缓存,立刻给记录下来. 首先 我的 ...
- 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例
之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...
- 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache
本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...
- 3.2.3 SpringMVC注解式开发
SpringMVC注解式开发 1. 搭建环境 (1) 后端控制器无需实现接口 , 添加相应注解 Controller类添加注解 @Controller //该注解表将当前类交给spring容器管理 @ ...
随机推荐
- iOS:使用莱文斯坦距离算法计算两串字符串的相似度
Levenshtein:莱文斯坦距离 Levenshtein的经典算法,参考http://en.wikipedia.org/wiki/Levenshtein_distance的伪代码实现的,同时参考了 ...
- [JS]两个常用的取随机整数的函数
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- SSE图像算法优化系列一:一段BGR2Y的SIMD代码解析。
一个同事在github上淘到一个基于SIMD的RGB转Y(彩色转灰度或者转明度)的代码,我抽了点时间看了下,顺便学习了一些SIMD指令,这里把学习过程中的一些理解和认识共享给大家. github上相关 ...
- 后台任务hangfire
Installation¶ There are a couple of packages for Hangfire available on NuGet. To install Hangfire in ...
- 基于Centos搭建Python Web 环境搭建教程
CentOS 7.2 64 位操作系统 安装 setuptools 工具 安装 因为之后我们需要安装 Django ,而 Django 需要用这个工具,所以我们需要先安装 setuptools 工具. ...
- 【Windows】查看Windows上运行程序的异常日志
任何在windows系统上运行的程序,只要发生异常导致程序异常终止,windows都会在日志中详细记录这个异常.可以在计算机管理中查看,如图:也可以在操作中心查看,如图:
- Linux说明书 - man浅谈
原文链接: http://www.cnblogs.com/xuxn/archive/2011/08/26/linux-manual-man-command.html 所有学过Linux的同学都应该知道 ...
- java多线程之线程的同步与锁定(转)
一.同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏. 例如:两个线程ThreadA.ThreadB都操作同一个对象Foo对象,并修改Foo对象上的数据. publicc ...
- x-pack
x-pack安装>官网安装步骤https://www.elastic.co/downloads/x-pack >x-pack简介X-Pack是一个Elastic Stack的扩展,将安全, ...
- 译:4.RabbitMQ Java Client 之 Routing(路由)
在上篇博文 译:3.RabbitMQ 之Publish/Subscribe(发布和订阅) 我们构建了一个简单的日志系统 我们能够向许多接收者广播日志消息. 在本篇博文中,我们将为其添加一个功能 - ...