1.前置条件 项目里集成了redisson
controller 层
//查询
@GetMapping("/getUserById/{id}")
public Result getUserById(@PathVariable("id")Integer id) {
User user = userService.getUserById(id);
return Result.ok(UserCovertBasic.INSTANCE.toConvertUserVO(user));
}
//更新
@RequestMapping(value = "/updateUser")
public Result updateUser(@RequestBody @Validated UserUpdateDTO userUpdateDTO) throws Exception {
Long userId = userUpdateDTO.getId();
User user = userService.getById(userId);
if (BeanUtil.isEmpty(user)){
throw new Exception("用户不存在");
}
BeanUtils.copyProperties(userUpdateDTO,user);
userService.updateByUser(user);
return Result.ok();
}
service层
public interface IUserService extends IService<User> {
User getUserById(Integer id);
void updateByUser(User user);
}
service impl层
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
public final String CACHE_USER_BY_ID = "CACHE_USER_BY_ID";
@Resource
private RedisUtils redisUtils;
@Resource
private RedissonClient redissonClient;
@Override
public User getUserById(Integer id) {
String idStr = String.valueOf(id);
String lockKey = CACHE_USER_BY_ID.concat(idStr);
RReadWriteLock lock = redissonClient.getReadWriteLock(lockKey);
//查询的时候使用共享读锁
RLock readLock = lock.readLock();
readLock.lock();
User user = null;
try {
if (redisUtils.hHasKey(CACHE_USER_BY_ID, idStr)) {
String cache = JSONUtil.toJsonStr(redisUtils.hget(CACHE_USER_BY_ID, idStr));
return JSON.parseObject(cache, User.class);
}
user = this.getById(id);
if (null != user) {
redisUtils.hset(CACHE_USER_BY_ID, idStr, JSONUtil.toJsonStr(user));
}
} finally {
if (readLock.isLocked() && readLock.isHeldByCurrentThread()) {
readLock.unlock(); //解锁
}
}
return user;
}
@Override
public void updateByUser(User user) {
String idStr = String.valueOf(user.getId());
String lockKey = CACHE_USER_BY_ID.concat(idStr);
//获取redisson 读写锁
RReadWriteLock lock = redissonClient.getReadWriteLock(lockKey);
//更新时使用独占写锁
RLock rLock = lock.writeLock();
rLock.lock();
try {
this.updateById(user);
redisUtils.hset(CACHE_USER_BY_ID, idStr, JSONUtil.toJsonStr(user));
} finally {
if (rLock.isLocked() && rLock.isHeldByCurrentThread()) {
rLock.unlock(); //3. 解锁
}
}
}
}
通过上面的redisson 的ReadWriteLock特性,在查询同一个用户的时候,使用的同一把lockKey,在读的时候共享读,写的时候独占写。保证了缓存和数据的一致性。
我在修改的时候,你的读阻塞等待锁。
你在读的时候,我的写串行排队等待锁。
一起都是读的时候,不阻塞。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/65733.html