一、使用方法参数
"#参数名"或者"#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对象
![](https://img.haomeiwen.com/i8100269/13251da39681f156.png)
"#root"可以省略
三、几种默认情况验证
1、不指定key且方法没有参数
@Cacheable(cacheNames = "DBcache"):
![](https://img.haomeiwen.com/i8100269/d6ada737a6779efa.png)
key为SimpleKey对象
2、不指定key且参数为1个int
![](https://img.haomeiwen.com/i8100269/d39145e51c3acb39.png)
3、不指定key且参数为2个int
![](https://img.haomeiwen.com/i8100269/d7f76b560816e0e0.png)
4、不指定key且参数为一个实体
![](https://img.haomeiwen.com/i8100269/9b92b43b84d6df5d.png)
key为对象本身
5、指定一个key
@Cacheable(cacheNames = "DBcache",key = "methodName")
![](https://img.haomeiwen.com/i8100269/38627b5f32444a69.png)
@Cacheable(cacheNames = "DBcache",key = "targetClass+'_'+methodName")
![](https://img.haomeiwen.com/i8100269/1f9776a20e87ebf3.png)
四、总结
1、缓存的key最好是自定义,可以自定义KeyGenerator
2、缓存一些无参的常用列表,分页情况不适用
网友评论