1. @Cacheable
Cacheable 注解不带参数时,是以方法参数值的组合作为key值,那么此时就会出现问题,即当两个方法的参数类型、个数都是一样时,如果两个方法在调用时传入的参数值也一样,那么就会出现数据读取错误的情况,所以指定缓存的key非常重要。
key如何指定呢?:
参考链接:https://blog.csdn.net/BinshaoNo_1/article/details/84579326
key指定用的是Spring EL表达式,这里的EL表达式可以使用方法参数及参数是对象时,它们对应的成员变量。
使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。
@Cacheable(value="users", key="#id")
public User find(Integer id) {
return null;
}
@Cacheable(value="users", key="#p0")
public User find(Integer id) {
return null;
}
@Cacheable(value="users", key="#user.id")
public User find(User user) {