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;
    }
相关推荐
uhakadotcom1 分钟前
构建高效自动翻译工作流:技术与实践
后端·面试·github
腥臭腐朽的日子熠熠生辉6 分钟前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
Asthenia04127 分钟前
深入分析Java中的AQS:从应用到原理的思维链条
后端
ejinxian8 分钟前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之13 分钟前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
Asthenia041222 分钟前
如何设计实现一个定时任务执行器 - SpringBoot环境下的最佳实践
后端
圈圈编码33 分钟前
Spring Task 定时任务
java·前端·spring
兔子的洋葱圈41 分钟前
【django】1-2 django项目的请求处理流程(详细)
后端·python·django
俏布斯1 小时前
算法日常记录
java·算法·leetcode
Asthenia04121 小时前
如何为这条sql语句建立索引:select * from table where x = 1 and y < 1 order by z;
后端