Redis 实现分布式锁
private RedisTemplate redisTemplate; @Override public OperationOrderEntity edit(OperationOrderEntity entity) { //如果返回false,则说明该key值存在,已经有程序在使用这个key值,从而实现了分布式加锁的功能 if (!redisTemplate.opsForValue().setIfAbsent(EDIT, new Date(), 10, TimeUnit.MINUTES)) { return null; } try { entity.setUpdateBy(getUsername()); entity.setUpdateName(getUserNickName()); entity.setUpdateTime(new Date()); entity.setStateName(OperationOrderStateEnum.getNameByCode(entity.getState())); LambdaQueryWrapper<OperationOrderEntity> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(OperationOrderEntity::getOperationNumber, entity.getOperationNumber()); this.update(entity, queryWrapper); } catch (Exception e) { log.error("编辑保存失败:", e); } finally { redisTemplate.delete(EDIT); } return entity; }
pom 依赖
<!--Redis缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>