redis工具类优化

This commit is contained in:
wr
2026-04-23 14:37:24 +08:00
parent c58bd87a78
commit 499eee6784

View File

@@ -2,9 +2,7 @@ package com.njcn.redis.utils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
@@ -325,8 +323,25 @@ public class RedisUtil {
* @return
*/
public List<?> getLikeListAllValues(String key) {
List<Object> info=new ArrayList<>();
for (String s : redisTemplate.keys(key + "*")) {
List<Object> info = new ArrayList<>();
// 使用 SCAN 替代 keys解决 Tair 禁用 KEYS 命令的问题
Set<String> keys = redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
Set<String> keySet = new HashSet<>();
ScanOptions options = ScanOptions.scanOptions()
.match(key + "*")
.count(1000)
.build();
try (Cursor<byte[]> cursor = connection.scan(options)) {
while (cursor.hasNext()) {
keySet.add(new String(cursor.next()));
}
} catch (Exception e) {
throw new RuntimeException("Redis SCAN 模糊查询失败", e);
}
return keySet;
});
for (String s : keys) {
info.add(redisTemplate.opsForValue().get(s));
}
return info;