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;
    }
相关推荐
爱学习的小可爱卢22 分钟前
JavaEE进阶——SpringMVC响应处理详解
spring boot·postman·javaee
q***96581 小时前
Spring Cloud Data Flow 简介
后端·spring·spring cloud
7***68431 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
L***d6701 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
凌波粒1 小时前
Springboot基础教程(3)--自动装配原理/静态资源处理/欢迎页
java·spring boot·后端
likuolei1 小时前
XSL-FO 软件
java·开发语言·前端·数据库
凌波粒1 小时前
SpringBoot基础教程(2)--yaml/配置文件注入/数据校验/多环境配置
java·spring boot·后端·spring
S***26751 小时前
Spring Boot环境配置
java·spring boot·后端
6***83052 小时前
什么是Spring Boot 应用开发?
java·spring boot·后端
毕设源码柳学姐2 小时前
计算机毕设 java 智慧社区服务系统 SSM 框架社区生活平台 Java 开发的便民服务与互动系统
java·开发语言·生活