feat(cache/redisson): RedisUtils 新增递增、递减方法

This commit is contained in:
2024-05-13 21:57:42 +08:00
parent b5dd5c7f18
commit 596605b27b
3 changed files with 27 additions and 8 deletions

View File

@@ -74,6 +74,6 @@ public class JustAuthStateCacheRedisImpl implements AuthStateCache {
*/
@Override
public boolean containsKey(String key) {
return RedisUtils.hasKey(RedisUtils.formatKey(KEY_PREFIX, key));
return RedisUtils.exists(RedisUtils.formatKey(KEY_PREFIX, key));
}
}

View File

@@ -22,7 +22,6 @@ import top.continew.starter.core.constant.StringConstants;
import java.time.Duration;
import java.util.Collection;
import java.util.stream.Stream;
/**
* Redis 工具类
@@ -92,6 +91,28 @@ public class RedisUtils {
CLIENT.getKeys().deleteByPattern(pattern);
}
/**
* 递增 1
*
* @param key 键
* @return 当前值
* @since 2.0.1
*/
public static long incr(String key) {
return CLIENT.getAtomicLong(key).incrementAndGet();
}
/**
* 递减 1
*
* @param key 键
* @return 当前值
* @since 2.0.1
*/
public static long decr(String key) {
return CLIENT.getAtomicLong(key).decrementAndGet();
}
/**
* 设置缓存过期时间
*
@@ -130,9 +151,8 @@ public class RedisUtils {
* @param key 键
* @return true存在false不存在
*/
public static boolean hasKey(String key) {
RKeys keys = CLIENT.getKeys();
return keys.countExists(key) > 0;
public static boolean exists(String key) {
return CLIENT.getKeys().countExists(key) > 0;
}
/**
@@ -142,8 +162,7 @@ public class RedisUtils {
* @return 缓存列表
*/
public static Collection<String> keys(String pattern) {
Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern);
return stream.toList();
return CLIENT.getKeys().getKeysStreamByPattern(pattern).toList();
}
/**

View File

@@ -36,7 +36,7 @@ public class BehaviorCaptchaCacheServiceImpl implements CaptchaCacheService {
@Override
public boolean exists(String key) {
return RedisUtils.hasKey(key);
return RedisUtils.exists(key);
}
@Override