spring boot 配置cacheManager EnableCaching EnableCaching

这篇具有很好参考价值的文章主要介绍了spring boot 配置cacheManager EnableCaching EnableCaching。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

fast json 的配置 cacheManage

@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 


    /**
     * 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;
    }

文章来源地址https://www.toymoban.com/news/detail-597018.html

到了这里,关于spring boot 配置cacheManager EnableCaching EnableCaching的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 【Spring Boot】Spring Boot配置文件详情

    【Spring Boot】Spring Boot配置文件详情

     Spring Boot是一个开源的Java框架,用于快速构建应用程序和微服务。它基于Spring Framework,通过自动化配置和约定优于配置的方式,使开发人员可以更快地启动和运行应用程序。Spring Boot提供了许多开箱即用的功能和插件,包括嵌入式Web服务器、安全性、数据访问、缓存、测试

    2024年02月12日
    浏览(8)
  • Spring Boot——Spring Boot自动配置原理

    Spring Boot——Spring Boot自动配置原理

    Spring Boot启动原理 一直在使用Spring Boot特别好奇的是为什么Spring Boot比Spring在项目构建和开发过程中要方便很多,无需编写大量的配置,Spring Boot自动给你配置好了。往往是集成项目依赖之后一键使用。于是小编我就学习和研究了一下Spring Boot的自动配置。 主程序入口示例 :

    2024年02月15日
    浏览(8)
  • Spring —— Spring Boot 配置文件

    Spring —— Spring Boot 配置文件

    JavaEE传送门 JavaEE Spring —— Bean 作用域和生命周期 Spring —— Spring Boot 创建和使用 如果没有配置信息, Spring Boot 项目就不能连接和此操作数据库, 甚至是不能保存可以用于排查问题的关键日志, 配置文件的作用是非常重要的. 系统使用的配置文件 (系统配置文件), 如端口号的配

    2023年04月09日
    浏览(12)
  • 【Spring Boot学习一】创建项目 && Spring Boot的配置文件

    【Spring Boot学习一】创建项目 && Spring Boot的配置文件

    目录 一、安装插件 二、创建Spring Boot项目 1、创建项目 1.1 使用IDEA创建  1.2 网页版本创建 2、项目目录介绍与运行 三、Sping Boot的配置文件(重点) 🌷1、.properties配置文件 (1)基础语法:Key = value (2)读取配置⽂件中的内容,@Value 注解使⽤“${}”的格式读取; 🌷2、.y

    2024年02月16日
    浏览(13)
  • 【Spring Boot】Spring Boot 配置 Hikari 数据库连接池

    数据库连接池是一个提高程序与数据库的连接的优化,连接池它主要作用是提高性能、节省资源、控制连接数、连接管理等操作; 程序中的线程池与之同理,都是为了优化、提高性能。

    2024年02月11日
    浏览(16)
  • 【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理

    【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理

    💓 博客主页:从零开始的-CodeNinja之路 ⏩ 收录文章:【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理 🎉欢迎大家点赞👍评论📝收藏⭐文章 配置文件主要是为了解决硬编码带来的问题,把可能会发生改变的信息,放在⼀个集中的地方,当我们启 动某个程序时,应用程

    2024年04月23日
    浏览(13)
  • Spring boot日志配置

    Spring boot日志配置

    Spring Boot 底层默认使用 slf4j 和 logback 的方式记录日志。工程中依赖了 spring-boot-starter-web,它又依赖了 spring-boot-starter-logging,所以不需要再手动添加该依赖。在 Spring Boot 中,application.yml 支持部分 logback 的日志配置,但一些高级配置只能通过独立的 xml 配置文件实现,经过 Sp

    2024年02月07日
    浏览(13)
  • spring boot 自动配置

    spring boot 自动配置

    自动配置介绍 Spring Boot自动装配(Auto Configuration)是Spring Boot框架的一个关键特性,它通过约定大于配置的方式来简化项目的配置过程。自动装配允许开发人员使用默认的配置,同时也可以根据需要进行定制化。Spring通过使用 @Autowired 注解、 @ComponentScan 注解以及条件化配置等

    2024年01月25日
    浏览(12)
  • Spring Boot配置文件

    Spring Boot配置文件

    日升时奋斗,日落时自省  目录 1、配置文件作用 2、配置文件格式 2.1、使用注意 3、properties配置文件 3.1、注释中文问题 3.2、properties语法格式 3.3、读取配置文件 3.3.1、Value读取 3.3.2、PropertySource读取 3.3.3、原生方式读取配置文件 3.4、properties缺点分析 4、yml配置文件 4.1、优点

    2024年02月01日
    浏览(10)
  • Spring Boot3 系列:Spring Boot3 跨域配置 Cors

    CORS,全称是“跨源资源共享”(Cross-Origin Resource Sharing),是一种Web应用程序的安全机制,用于控制不同源的资源之间的交互。 在Web应用程序中,CORS定义了一种机制,通过该机制,浏览器能够限制哪些外部网页可以访问来自不同源的资源。源由协议、域名和端口组成。当一

    2024年01月19日
    浏览(13)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包