cacheNames给缓存一个名字,相当于定义了一个容器
cacheNames
|
key的命名:
名字 | 描述 | 示例 |
methodName | 当前被调用的方法名 | #root.methodName |
target | 当前被调用的目标对象 | #root.target |
targetClass | 当前被调用的目标对象类 | #root.targetClass |
args | 当前被调用的方法的参数列表 | #root.args[0] |
caches | 当前方法调用使用的缓存列表(如@Cacheable(value= {"cache1", "cache2"})),则有两个cache | #root.caches[0].name |
argument name | 方法参数的名字. 可以直接 #参数名 ,也可以使用 #p0或#a0 的形式,0代表参数的索引; | #iban 、 #a0 、 #p0 |
result | 方法执行后的返回值(仅当方法执行之后的判断有效,在 @CachePut 使用于更新数据后可用) | #result |
举例:
package com.cc.springboot.service;
import com.cc.springboot.entities.User;
import com.cc.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
@Cacheable(cacheNames = "user",key = "#id")
public User getUserById(Integer id){
User user=userMapper.getUserById(id);
return user;
}
}