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;
    }
相关推荐
fly spider4 分钟前
每日 Java 面试题分享【第 13 天】
java·开发语言·面试
Pandaconda9 分钟前
【Golang 面试题】每日 3 题(四十三)
开发语言·经验分享·笔记·后端·面试·golang·go
兮动人11 分钟前
Go语言快速开发入门
开发语言·后端·golang·go语言快速开发入门
大名顶顶17 分钟前
【JAVA实战】如何使用 Apache POI 在 Java 中写入 Excel 文件
java·spring boot·后端·计算机·程序员·编程·软件开发
gentle_ice1 小时前
leetcode——矩阵置零(java)
java·算法·leetcode·矩阵
Future_yzx1 小时前
基于SpringBoot+WebSocket的前后端连接,并接入文心一言大模型API
spring boot·websocket·文心一言
stevewongbuaa2 小时前
一些烦人的go设置 goland
开发语言·后端·golang
whisperrr.2 小时前
【JavaWeb06】Tomcat基础入门:架构理解与基本配置指南
java·架构·tomcat
火烧屁屁啦3 小时前
【JavaEE进阶】应用分层
java·前端·java-ee