| | |
| | | return redisTemplate.opsForValue().increment(key); |
| | | } |
| | | |
| | | /**查数据库 |
| | | * 获取指定日期在今日的数值,并自增1 |
| | | * @return 目标日期自增后的计数值 |
| | | */ |
| | | public long incrementAndGetByDb(LocalDate targetDate) { |
| | | if (targetDate == null) { |
| | | targetDate = LocalDate.now(); |
| | | } |
| | | String dateStr = targetDate.format(DATE_FORMAT); |
| | | String key = approvalNumberPrefix + ":approveNum:" + dateStr; |
| | | String lockKey = "lock:" + key; |
| | | |
| | | if (Boolean.FALSE.equals(redisTemplate.hasKey(key))) { |
| | | Boolean acquired = redisTemplate.opsForValue().setIfAbsent(lockKey, "1", 10, TimeUnit.SECONDS); |
| | | if (Boolean.TRUE.equals(acquired)) { |
| | | try { |
| | | if (Boolean.FALSE.equals(redisTemplate.hasKey(key))) { |
| | | LambdaQueryWrapper<ApproveProcess> approveProcessLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | approveProcessLambdaQueryWrapper |
| | | .eq(ApproveProcess::getApproveDelete, 0) |
| | | .likeRight(ApproveProcess::getApproveId, dateStr); |
| | | Long count = approveProcessMapper.selectCount(approveProcessLambdaQueryWrapper); |
| | | long initialCount = (count == null) ? 0 : count; |
| | | |
| | | redisTemplate.opsForValue().set(key, String.valueOf(initialCount), 24, TimeUnit.HOURS); |
| | | } |
| | | } finally { |
| | | // 释放锁 |
| | | redisTemplate.delete(lockKey); |
| | | } |
| | | } else { |
| | | try { |
| | | Thread.sleep(100); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | } |
| | | return incrementAndGetByDb(targetDate); // 递归重试 |
| | | } |
| | | } |
| | | |
| | | return redisTemplate.opsForValue().increment(key); |
| | | } |
| | | |
| | | /** |
| | | * 获取指定计数器在今日的当前数值 |
| | | * @param counterName 计数器名称 |