public static void incrementAndGet(RedissonClient redissonClient, String redisKey, Long epochSecond) {
RAtomicLong atomicLong = redissonClient.getAtomicLong(redisKey);
if (atomicLong.get() == 0) {
atomicLong.expire(epochSecond, TimeUnit.SECONDS);
}
long cont = atomicLong.incrementAndGet();
}
问题设置时间失效,但是
atomicLong.expire(epochSecond, TimeUnit.SECONDS); 也执行了 。但是没有生效 解决,修改一行代码
原因 key不存在,所以设置过期时间失败,所以先+1,在赋值失效时间
public static void incrementAndGet(RedissonClient redissonClient, String redisKey, long epochSecond) {
RAtomicLong atomicLong = redissonClient.getAtomicLong(redisKey);
long cont = atomicLong.incrementAndGet();
if (atomicLong.get() == 1) {
atomicLong.expire(epochSecond, TimeUnit.SECONDS);
}
log.info("计数key:{}-{}", redisKey, cont);
}