缓存设计模式
缓存热点数据, 缩短调用链
旁路缓存
适用于强一致性
- 写请求 -> 更新DB, 并删除cache
- 读请求 -> 先查cache, miss后再查DB并写回
@Service
public class UserService{
@Autowired
private UserDAO userDAO;
private RedisTemplate redisTemplate;
public User getUserById(Integer id) {
User user = redisTemplate.opForObject();
if (user == null) {
user = userDAO.getUserById(id);
if (user != null) {
redisTemplate.update();
}
}
}
public void updateUser(User user) {
Integer id = user.getId();
userDAO.updateUser(user);
redisTemplate.delete(id);
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/288702.html