赞
踩
- //使用redis 缓存
- //1.优先从缓冲中取值
- public User getCache(int userId){
- String redisKey = RedisKeyUtils.getUserKey(userId);
- User user = (User) redisTemplate.opsForValue().get(redisKey);
- return user;
- }
- //2.取不到时初始化缓存数据
- public User initCache(int userId){
- String redisKey = RedisKeyUtils.getUserKey(userId);
- User user = userMapper.selectById(userId);
- redisTemplate.opsForValue().set(redisKey, user,3600, TimeUnit.SECONDS);
- return user;
- }
- //3.数据变更时删除缓存数据
- public void clearCache(int userId){
- String redisKey = RedisKeyUtils.getUserKey(userId);
- redisTemplate.delete(redisKey);
- }
-
-
- public User findUserById(int id){
- // User user = userMapper.selectById(id);
- User user = getCache(id);
- if(user == null){
- user = initCache(id);
- }
- return user;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。