• spring cache Redis 配置自定义过期时间


    自定义RedisCacheConfig

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.interceptor.KeyGenerator;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.cache.RedisCacheWriter;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    
    import java.time.Duration;
    import java.util.HashMap;
    import java.util.Map;
    
    @Configuration
    public class RedisCacheConfig {
        /**
         * MONTH_TIMEOUT 指定 key 过期时间 1个月
         */
        private static int MONTH_TIMEOUT = 2592000;
        /**
         * DAY_TIMEOUT 指定 key 过期时间 1天
         */
        private static int DAY_TIMEOUT = 86400;
        /**
         * DEFAULT_TIMEOUT_HOUR 默认 1小时过期
         */
        private static int DEFAULT_TIMEOUT_HOUR = 3600;
    
        @Bean
        public KeyGenerator simpleKeyGenerator() {
            return (o, method, objects) -> {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(o.getClass().getSimpleName());
                stringBuilder.append(".");
                stringBuilder.append(method.getName());
                stringBuilder.append("[");
                for (Object obj : objects) {
                    stringBuilder.append(obj.toString());
                }
                stringBuilder.append("]");
    
                return stringBuilder.toString();
            };
        }
    
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
            return new RedisCacheManager(
                    RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
                    // 默认策略,未配置的 key 会使用这个
                    this.getRedisCacheConfigurationWithTtl(DEFAULT_TIMEOUT_HOUR),
                    // 指定 key 策略
                    this.getRedisCacheConfigurationMap()
            );
        }
    
        private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
            Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
            redisCacheConfigurationMap.put("touchpoint:zone:getZoneList", this.getRedisCacheConfigurationWithTtl(MONTH_TIMEOUT));
            redisCacheConfigurationMap.put("touchpoint:zone:getSublevelAreaInfoByCode", this.getRedisCacheConfigurationWithTtl(MONTH_TIMEOUT));
            redisCacheConfigurationMap.put("touchpoint:dict:getServiceDict", this.getRedisCacheConfigurationWithTtl(MONTH_TIMEOUT));
            redisCacheConfigurationMap.put("touchpoint:org:getOrgListByUserId", this.getRedisCacheConfigurationWithTtl(DAY_TIMEOUT));
            return redisCacheConfigurationMap;
        }
    
        private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
    
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
            redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                    RedisSerializationContext
                            .SerializationPair
                            .fromSerializer(jackson2JsonRedisSerializer)
            ).entryTtl(Duration.ofSeconds(seconds));
    
            return redisCacheConfiguration;
        }
    }
    

    常用命令

    缓存

    @Cacheable(cacheNames = "touchpoint:dict:getServiceDict", key = "#param.moudle+'-'+#param.dictCode")
    

    将对应的cacheName全部删除

    @CacheRemoveAll(cacheName = "touchpoint:dict:getServiceDict")
    

    删除指定可以

    @CacheEvict(cacheNames = "touchpoint:dict:getServiceDict", key = "#param.moudle+'-'+#param.dictCode")
    
  • 相关阅读:
    Winform中实现ZedGraph曲线图缩放后复原功能
    MySQL Workbench 安装失败 Mysql workbench requires the visual C++ 2019 redistributable package
    MySQL Workbench 8.0提示SSL connection error: SSL is required but the server doesn‘t support it
    域名证书有效,但是访问提示不安全连接
    图片Base64编码
    centos系统设置防火墙
    《中有成就秘笈》之中央密严刹土
    Arweave
    去中心化身份聚合器
    区块链跨链技术
  • 原文地址:https://www.cnblogs.com/xionghaizhi/p/16354298.html
Copyright © 2020-2023  润新知