| | |
| | | long count = redisTemplate.opsForValue().increment(key, 1); |
| | | |
| | | // 仅在第一次设置时设置过期时间(避免重复设置) |
| | | if (count == 1) { |
| | | long secondsUntilMidnight = calculateSecondsUntilMidnight(); |
| | | redisTemplate.expire(key, secondsUntilMidnight, TimeUnit.SECONDS); |
| | | } |
| | | // if (count == 0) { |
| | | // long secondsUntilMidnight = calculateSecondsUntilMidnight(); |
| | | // redisTemplate.expire(key, secondsUntilMidnight, TimeUnit.SECONDS); |
| | | // } |
| | | |
| | | return count; |
| | | } |
| | |
| | | |
| | | /** |
| | | * 生成Redis键 |
| | | * 格式 daily_counter:counterName:yyyyMMdd |
| | | */ |
| | | private String getKey(String counterName) { |
| | | String today = LocalDate.now().format(DATE_FORMAT); |