@Cacheable key
2019-06-25 本文已影响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对象

"#root"可以省略
三、几种默认情况验证
1、不指定key且方法没有参数
@Cacheable(cacheNames = "DBcache"):

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

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

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

key为对象本身
5、指定一个key
@Cacheable(cacheNames = "DBcache",key = "methodName")

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

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