liusheng
15 小时以前 c24d16a37523a49080392610357872ce580cbc38
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java
@@ -47,6 +47,23 @@
    }
    /**
     * 仅当 key 不存在时写入(SETNX 语义),用于<b>幂等/防重复提交</b>
     * <p>
     * 2026-10-21 新增:问卷提交接口({@code /smartor/subtaskAnswer/saveQuestionAnswer})是
     * {@code @Async} 异步处理,患者端重复点击/网络重试会并发写多份明细,需要用原子占位挡掉。
     *
     * @param key      Redis 键
     * @param value    写入的值
     * @param timeout  过期时间
     * @param timeUnit 时间颗粒度
     * @return {@code true} = 占位成功(本次应继续处理);{@code false} = 已存在(重复请求,应忽略)
     */
    public <T> boolean setCacheObjectIfAbsent(final String key, final T value, final long timeout, final TimeUnit timeUnit) {
        Boolean ok = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
        return Boolean.TRUE.equals(ok);
    }
    /**
     * 设置有效时间
     *
     * @param key     Redis键
@@ -310,6 +327,20 @@
     * @return
     */
    public Long getListSize(String key) {
        // 检查 key 类型
        DataType keyType = redisTemplate.type(key);
        // 如果 key 不存在,返回 0
        if (keyType == DataType.NONE) {
            return 0L;
        }
        // 如果不是 List 类型,删除它并返回 0(防止 WRONGTYPE 错误)
        if (keyType != DataType.LIST) {
            redisTemplate.delete(key);
            return 0L;
        }
        return redisTemplate.opsForList().size(key);
    }
}