我正在尝试使用 Spring @Cacheable 注释。
@Cacheable(value="users")
public List<User> findAll() {
System.out.println("Looking for All users : ");
return userRepository.findAll();
}
@Override
@Cacheable(value="users")
public User findOne(String userId) {
System.out.println("Looking for user : "+ userId);
return userRepository.findById(userId).get();
}
Run Code Online (Sandbox Code Playgroud)
当我执行第一个方法时,List<User>
我得到:
- 第一次:从数据库中选择所有字段
- 第二次:从缓存中选择所有字段
- 第三次:从缓存中选择所有字段
到目前为止这都很好。
当我执行第二种方法时,findOne(String userId)
我得到的结果是:
- 第一次:从数据库中选择特定字段
- 第二次:从缓存中选择特定字段
- 第三次:从缓存中选择特定字段
这又好了。
当我执行第一个方法时,List<User>
我得到:
从缓存中选择所有字段数据
问题:这两种方法(和 )如何具有相同的缓存名称,但返回不同的结果。List<User>
findOne(String userId)