美文网首页
@Cacheable key

@Cacheable key

作者: 晚歌歌 | 来源:发表于2019-06-25 19:04 被阅读0次

一、使用方法参数

"#参数名"或者"#p参数index"

  @Cacheable(value="users", key="#id")
   public User find(Integer id) {
   }

   @Cacheable(value="users", key="#p0")
   public User find(Integer id) {
   }

   @Cacheable(value="users", key="#user.id")
   public User find(User user) {
   }

   @Cacheable(value="users", key="#p0.id")
   public User find(User user) {
   }

二、使用内置root对象

image.png

"#root"可以省略

三、几种默认情况验证

1、不指定key且方法没有参数

@Cacheable(cacheNames = "DBcache"):


image.png

key为SimpleKey对象

2、不指定key且参数为1个int

image.png

3、不指定key且参数为2个int

image.png

4、不指定key且参数为一个实体

image.png

key为对象本身

5、指定一个key

@Cacheable(cacheNames = "DBcache",key = "methodName")


image.png

@Cacheable(cacheNames = "DBcache",key = "targetClass+'_'+methodName")


image.png

四、总结

1、缓存的key最好是自定义,可以自定义KeyGenerator
2、缓存一些无参的常用列表,分页情况不适用

相关文章

网友评论

      本文标题:@Cacheable key

      本文链接:https://www.haomeiwen.com/subject/htpxcctx.html