SpringBoot:基于注解的@CachePut

SpringBoot基于注解的@CachePut

几个重要的概念及注解

名称 解释
Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、ConcurrentMapCache等
CacheManager 缓存管理器,管理各种缓存(cache)组件
@Cacheable 主要针对方法配置,能够根据方法的请求参数对其进行缓存
@CacheEvict 清空缓存
@CachePut

保证方法被调用,又希望结果被缓存。
与@Cacheable区别在于是否每次都调用方法,常用于更新
无论怎样,@CachePut总将方法的返回值放到缓存中

@EnableCaching 开启基于注解的缓存
keyGenerator 缓存数据时key生成策略
serialize 缓存数据时value序列化策略
@CacheConfig 统一配置本类的缓存注解的属性
 

  部分代码:

  
 1 //Controller层
 2     @RequestMapping("/select")
 3     @ResponseBody
 4     public String select(int id) {
 5         return gson.toJson(telephonebookService.selectByPrimaryKey(id));
 6     }
 7 
 8     @RequestMapping("/update")
 9     @ResponseBody
10     public String update(String dp, String dt) {
11         Telephonebook tbook = new Telephonebook(1,"李四",dp,dt,"FSS");
12         return gson.toJson(telephonebookService.updateByPrimaryKey(tbook));
13     }
14 //ServiceImpl
15 @CacheConfig(cacheNames = "tbook")
16 class ……{
17     ……
18 
19     @Override
20     @CachePut(key = "#record.getId()")
21     public Telephonebook updateByPrimaryKey(Telephonebook record) {
22         telephonebookMapper.updateByPrimaryKey(record);
23         return record;
24     }
25     @Override
26     @Cacheable(key = "#id")
27     public Telephonebook selectByPrimaryKey(Integer id) {
28         return telephonebookMapper.selectByPrimaryKey(id);
29     }
30 }
View Code

  @CacheConfig(cacheNames = "tbook") 抽取本类的公共value

  @CachePut 是将“方法返回”的值放入缓存中,被注解的方法返回的值是什么,则它存放的值就是什么

  

猜你喜欢

转载自www.cnblogs.com/itcod/p/12511126.html