From 5ae0c76f29e053169e349856259cfb63ee9c3d8f Mon Sep 17 00:00:00 2001 From: wj <1285151836@qq.com> Date: Thu, 13 Apr 2023 11:02:26 +0800 Subject: [PATCH] =?UTF-8?q?RedisUtil.java=E6=96=B0=E5=A2=9EsetIfAbsent?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/glxp/api/util/RedisUtil.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/com/glxp/api/util/RedisUtil.java b/src/main/java/com/glxp/api/util/RedisUtil.java index 51c55a02..44f50351 100644 --- a/src/main/java/com/glxp/api/util/RedisUtil.java +++ b/src/main/java/com/glxp/api/util/RedisUtil.java @@ -603,4 +603,20 @@ public class RedisUtil { } return Collections.emptyList(); } + + + /** + * 不存在key则缓存放入并设置时间 + * + * @param key 键 + * @param value 值 + * @param time 过期时间 (秒) + * @return boolean true为set成功,false为key已存在set失败 + */ + public boolean setIfAbsent(String key, Object value, long time) { + if (StrUtil.isEmpty(key)) { + return false; + } + return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS)); + } }