All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.redis.config.CustomizedRedisCache Maven / Gradle / Ivy

There is a newer version: 2.0.1.RELEASE
Show newest version
package com.luues.redis.config;

import cn.luues.tool.core.util.StrUtil;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheWriter;

public class CustomizedRedisCache extends RedisCache {

    private final String name;
    private final RedisCacheWriter cacheWriter;
    private final ConversionService conversionService;

    protected CustomizedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) {
        super(name, cacheWriter, cacheConfig);
        this.name = name;
        this.cacheWriter = cacheWriter;
        this.conversionService = cacheConfig.getConversionService();
    }

    @Override
    public void evict(Object key) {
        if(key instanceof String){
            if(StrUtil.endWith(key.toString(), "*")){
                evictLikeSuffix(key.toString());
                return;
            }
            if(StrUtil.startWith(key.toString(), "*")){
                evictLikePrefix(key.toString());
                return;
            }
        }
        super.evict(key);
    }
    protected void evictLikePrefix(String key){
        byte[] pattern = this.conversionService.convert(this.createCacheKey(key), byte[].class);
        this.cacheWriter.clean(this.name, pattern);
    }
    protected void evictLikeSuffix(String key){
        byte[] pattern = this.conversionService.convert(this.createCacheKey(key), byte[].class);
        this.cacheWriter.clean(this.name, pattern);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy