@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容器管理 @ ...
随机推荐
- VMWare Station 问题汇总
1.开机黑屏,不启动系统 解决方法: 命令行窗口cmd—输入下面代码,然后重启计算机. netsh winsock reset 2.提示磁盘被锁无法打开 解决方法: 虚拟机目录下面的.lck文件都删了
- MDX Cookbook 03 - MDX 查询中负数,零和空值 NULL 的格式化处理
FORMAT_STRING 属性在处理计算成员(通常是度量值成员)的时候会经常使用到,比如指定标准 Standard, 货币 Currency 或者 Percent 百分比格式.除此之外,还可以自定义 ...
- shell while内获取外部变量内容
一.问题 问题很简单,看下面一段tmp.sh代码: #!/bin/sh x="this is the initial value of x" cat /tmp/tmp | whil ...
- ZOJ 3827 Information Entropy 水
水 Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Informati ...
- 如果你喜欢python,那你迟早会喜欢上julia的!
你可曾想过有那么一门语言: 这门语言能够有C语言一样的速度,Ruby一样得活力(dynamism).像homoiconic一样的语言,它像Lisp一样有宏,但是也像Matlab一样有显而易见.熟悉的数 ...
- Verilog TestBench Coding Style
Abtract 关于编写testbench的一些经验总结心得. Introduction 1.基本的Testbench结构 1)常用的编码结构 `timescale 1 ns / 1 ps ...
- [Big Data - Codis] Codis集群的搭建与使用
一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...
- Git应用实践(一)
[时间:2017-03] [状态:Open] [关键词:Git,ssh,远程仓库,git remote] 0-背景 近期在使用Git@oschina上发现以下两个问题: 我的提交有两个名和email, ...
- 在layui layer 弹出层中加载 layui table
layui.use('table', function(){ var table = layui.table; layer.open({ type : 1, area : [ "600px& ...
- 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-5 底层驱动之旋转编码器
源视频包下载地址:链接:http://pan.baidu.com/s/1mhENI9i密码:mf1x 银杏科技优酷视频发布区:http://i.youku.com/gingko8