Files
cutejava/docs/help/缓存注解使用.txt
T

27 lines
733 B
Plaintext

@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Cacheable(value = "users", key = "#userId")
public User getUserById(Long userId) {
return userRepository.findById(userId).orElse(null);
}
@CachePut(value = "users", key = "#user.id")
// @CachePut(value = "users", key = "#user.id + '_' + #user.age")
public User updateUser(User user) {
userRepository.save(user);
return user;
}
@CacheEvict(value = "users", key = "#userId")
public void deleteUser(Long userId) {
userRepository.deleteById(userId);
}
@CacheEvict(value = "users", allEntries = true)
public void clearCache() {
// 清空缓存
}
}