fix
This commit is contained in:
parent
9dda80b4dd
commit
b24f0f4793
@ -6,7 +6,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,8 +25,10 @@ public class RedisFileCacheService implements FileCacheService {
|
|||||||
|
|
||||||
private final StringRedisTemplate redisTemplate;
|
private final StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
private static final String FILE_CACHE_PREFIX = "kb-file";
|
||||||
|
|
||||||
private String key(String prefix, String key) {
|
private String key(String prefix, String key) {
|
||||||
return String.format("kb:file:%s:%s", prefix, key);
|
return String.format(FILE_CACHE_PREFIX + ":%s:%s", prefix, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,7 +70,32 @@ public class RedisFileCacheService implements FileCacheService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearAll() {
|
public void clearAll() {
|
||||||
// 不清理 Redis 所有数据,仅为接口保留占位符
|
log.warn("正在清空 Redis 文件缓存,前缀: {}", FILE_CACHE_PREFIX);
|
||||||
log.warn("清空 Redis 文件缓存未实现,请手动清理前缀为 kb:file 的数据。");
|
Set<String> keysToDelete = scanKeys(FILE_CACHE_PREFIX + "*");
|
||||||
|
if (keysToDelete.isEmpty()) {
|
||||||
|
log.info("没有需要清理的文件缓存 Key。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
redisTemplate.delete(keysToDelete);
|
||||||
|
log.info("已清空 Redis 文件缓存 Key 数量: {}", keysToDelete.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用 scan 命令遍历所有带 prefix 的 key(推荐,不会阻塞大 Redis)
|
||||||
|
*/
|
||||||
|
private Set<String> scanKeys(String pattern) {
|
||||||
|
Set<String> keySet = new HashSet<>();
|
||||||
|
// 采用 scan,分批遍历,安全高效
|
||||||
|
redisTemplate.execute((connection) -> {
|
||||||
|
try (var cursor = connection.scan(org.springframework.data.redis.core.ScanOptions.scanOptions()
|
||||||
|
.match(pattern)
|
||||||
|
.count(1000)
|
||||||
|
.build())) {
|
||||||
|
cursor.forEachRemaining(item -> keySet.add(new String(item)));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}, false, true);
|
||||||
|
return keySet;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user