一、参考文档:
https://blog.csdn.net/qq_36194413/article/details/105833158
https://blog.csdn.net/llllllllll4er5ty/article/details/106337559
https://www.freesion.com/article/5258202702/
二、批量删除缓存代码
4.1 自定义RedisCache:(重写方法)
@Slf4j
public class CustomizedRedisCache extends RedisCache {
private static final String WILD_CARD = "*";
private final String name;
private final RedisCacheWriter cacheWriter;
private final ConversionService conversionService;
private RedisTemplate<String, Object> redisTemplate;
protected CustomizedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration, RedisTemplate<String,Object> redisTemplate){
super(name,cacheWriter,cacheConfiguration);
this.name = name;
this.cacheWriter = cacheWriter;
this.conversionService = cacheConfiguration.getConversionService();
this.redisTemplate = redisTemplate;
}
@Override
public void evict(Object key) {
if (key instanceof String){
String keyString = key.toString();
if (keyString.endsWith(WILD_CARD)){
evictLikeSuffix(keyString);
return;
}
if (keyString.startsWith(WILD_CARD)){
evictLikePrefix(keyString);
return;
}
}
super.evict(key);
}
/**
* 前缀匹配
* @param key
*/
public void evictLikePrefix(String key){
byte[] pattern = this.conversionService.convert(this.createCacheKey(key),byte[].class);
this.cacheWriter.clean(this.name,pattern);
}
public void evictLikeSuffix(String key){
byte[] pattern = this.conversionService.convert(this.createCacheKey(key),byte[].class);
this.cacheWriter.clean(this.name,pattern);
}
@Override
public void clear() {
LogUtils.info(log,"CustomizedRedisCache clear.......");
delRedisKeyByPre(this.name);
}
public void delRedisKeyByPre(String preKey){
long start = System.currentTimeMillis();
RedisCallback<Long> redisCallback = redisConnection -> {
String patternKey = preKey + "*";
ScanOptions options = ScanOptions.scanOptions().count(10000).match(patternKey).build();
Cursor<byte[]> cursor = redisConnection.scan(options