@Cacheable 、 @CachePut 、@CacheEvict 注解
使用场景
在开发中,需要使用缓存时,可以使用 @Cacheable 、 @CachePut 、@CacheEvict 注解。
注意:
@Cacheable 、 @CachePut 、@CacheEvict 是基于 Aop 动态代理实现的。
因此,如果在同一个类里面,调用 @Cacheable 注解的方法,缓存是不会生效的。
可以用 @Resource 在类里面注入同样的类,再用注入的类调用方法,就会走 Aop 代理。
或者是 将 @Cacheable 注解的方法 移到另一个类中。
在 Application 类上添加注解 @EnableCaching
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Cacheable 注解
能够让方法的返回值被缓存起来,后续的请求可以直接从缓存中获取结果。
示例:
@Cacheable( cacheManager = "cacheManagerTwoHour",
value = "cache:id:test",
key = "#id",
condition = "#id!=null")
public String getList(String id) {
//数据表查询
return configService.getNameById(id);
}
解释如下:
cacheManager :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。非必需。
value 是缓存key的前缀。
key 是缓存的key,其中的 #后面可以带上对象/参数。
condition 是条件。只有符合条件,缓存注解才会生效。
SpringCache注解,会自动拼接好缓存的key,并在中间加上符号:: ,
比如 value = "cache:id:test", key = "#id", 当参数id为12345时,那么真实的缓存 key 是
cache:id:test::12345
如果变更时使用 StringRedisTemplate 处理缓存,记得要把这个符号 :: 拼到缓存key里面。
方法参数为对象
示例:
如果 key 由多个字段组成,可以通过 # 指定对象字段,再拼接起来。
如果是所有的字段,可以用 #queryDto.toString().
@Cacheable(cacheManager = "cacheManagerTwoHour",
value = "cache:name:test:",
key = "#queryDto.userId + ':' + #queryDto.channel",
condition = "#queryDto.queryType == null or #queryDto.queryType==0")
public String getListByDto(QueryDto queryDto) {
//数据表查询
return configService.getNameById(id);
}
如何测试@Cacheable注解是否生效?
先查询一次该方法,然后修改数据库数据,再查询一次方法。
如果 @Cacheable 生效,那查出来的就是缓存的数据,而不是数据库的数据。
@CachePut
对key进行缓存,缓存的值为方法的返回值。可以在数据更新时使用。方法仍然会执行。
以下方法执行后,缓存的key为参数id,缓存对应的值为 entity。
@CachePut(
value = "cache:id:test",
key = "#id",
condition = "#id!=null")
public ConfigEntity update(String id) {
//数据表查询
ConfigEntity entity = new ConfigEntity();
entity.setWxBrandId("brandTest456");
entity.setId("12345");
configService.updateById(entity);
return entity;
}
@Cacheable和@CachePut 的区别:
@Cacheable: 当重复使用相同参数调用方法的时候,不会再次执行方法 ,
方法的结果直接从缓存中找到并返回了。
@CachePut: 方法一直会被执行,同时方法的返回值也被记录到缓存中。
@CacheEvict
(1)删除缓存中指定key的数据的。
@CacheEvict(
value = "cache:id:test",
key = "#id",
condition = "#id!=null")
public void update(String id) {
configService.updateById(id);
}
执行方法后,缓存中的 key及参数 就被删除了。
(2)redis 删除多个缓存:
@Caching(evict = {
@CacheEvict(cacheManager = "cacheManager", value = "prefix:key1", key = "#userId"),
@CacheEvict(cacheManager = "cacheManager", value = "prefix:key2", key = "#userId")})
public Integer updateStatus(String userId) {
return userService.updateStatus(userId);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-08-07 java并发:volatile关键字
2016-08-07 android笔记 : Content provider内容提供器