spring boot 配置cacheManager EnableCaching EnableCaching

复制代码
fast json 的配置 cacheManage
java 复制代码
@Configuration
public class ConfigRedisCacheManager {
    // 缓存自动过期时间
    private long expire = 4;

    static {
        ParserConfig.getGlobalInstance().addAccept("com");
        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
    }

    @Bean
    @Primary
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .disableCachingNullValues()
                .entryTtl(Duration.ofSeconds(expire))
                .serializeKeysWith(keySerializationPair())
                .serializeValuesWith(valueSerializationPair());

        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(cacheConfiguration)
                .build();
    }

    private RedisSerializationContext.SerializationPair<String> keySerializationPair() {
        return RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer());
    }

    private RedisSerializationContext.SerializationPair<Object> valueSerializationPair() {
        return RedisSerializationContext.SerializationPair.fromSerializer(new FastJsonRedisSerializer<>());
    }

    public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
        @Override
        public byte[] serialize(T t) throws SerializationException {
            if (t == null) {
                return new byte[0];
            }
            try {
                return JSON.toJSONBytes(t, SerializerFeature.WriteClassName);
                //return JSON.toJSONBytes(t);
            } catch (Exception ex) {
                throw new SerializationException("Could not write JSON: " + ex.getMessage(), ex);
            }
        }

        @SneakyThrows
        @Override
        public T deserialize(byte[] bytes) throws SerializationException {
            String data = new String(bytes, "UTF-8");
            T result = (T) JSON.parse(data);
            return result;
        }
    }
}

jackson 方式 配置 cacheManager

java 复制代码
    /**
     * RedisCacheConfiguration Bean
     *
     * 参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration 的 createConfiguration 方法
     */
    @Bean
    @Primary
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        // 设置使用 JSON 序列化方式
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();


        //config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));

        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
        objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING);
        serializer.setObjectMapper(objectMapper);
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));


        // 设置 CacheProperties.Redis 的属性
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
相关推荐
0x派大星9 分钟前
【Golang】——Gin 框架中的表单处理与数据绑定
开发语言·后端·golang·go·gin
喉咙痛的恐龙25 分钟前
C++之内存管理
java·jvm·c++
mit6.82434 分钟前
[Docker#11] 容器编排 | .yml | up | 实验: 部署WordPress
运维·后端·docker·云原生·容器
pumpkin8451436 分钟前
Django
后端·python·django
笑的像个child36 分钟前
IDEA优雅debug
java·ide·intellij-idea·idea
菜鸟谢1 小时前
PLC-读取中文字符串
后端
弗锐土豆1 小时前
maven工程修改jdk编译版本的几种方法
java·eclipse·maven·编译·版本
小码编匠1 小时前
WPF 实现 Element UI 风格的日期时间选择器
后端·c#·.net
尘浮生1 小时前
Java项目实战II基于Spring Boot的工作流程管理系统设计与实现(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·微信·微信小程序·小程序