• spring cache (Redis方式)



    前置

    会演示springcache的使用方式
    项目地址

    前置配置

    本篇文章是基于上篇文章进行: spring cache (默认方式)

    强调:

    需要使 Redis 配置(spring.cache.redis)生效, 以及自定义配置Redis的前缀 故添加配置文件: MyCacheConfig.java
    不使用MyCacheConfig.java, 需要 implements Serializable

    源码部分
    spring cache (默认方式) 一致, 只是实现缓存的方式不一样

    关键类: org.springframework.cache.Cache
    org.springframework.cache.interceptor.CacheInterceptor#invoke
    org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
    实现类: org.springframework.data.redis.cache.RedisCache

    相关缓存文章

    spring cache (默认方式)
    spring cache (Redis方式)
    spring cache (ehcache方式)


    pom: jar

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    
    <!-- 自定义二级缓存存储: Redis方式 -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置文件: application.yml

    spring:
      redis:
        database: 2
        host: 127.0.0.1
        port: 6379
        timeout: 5000
        jedis:
          pool:
            max-active: 100
            max-wait: -1
            max-idle: 10
            min-idle: 5
    
      cache:
        # 程序启动时创建的缓存名称
        cache-names: chaim-name
        # 缓存类型 org.springframework.boot.autoconfigure.cache.CacheType
        type: redis
        redis:
          # 缓存前缀
          key-prefix: "CACHE:"
          # 写入 Redis 时是否使用前缀 默认:true
          use-key-prefix: true
          # 允许缓存空值 默认: true, 可也防止缓存穿透问题
          cache-null-values: true
          # 缓存过期时间, 单位: 毫秒
          time-to-live: 300000
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    MyCacheConfig.java

    package com.chaim.spring.cache.redis.config;
    
    import org.springframework.boot.autoconfigure.cache.CacheProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    /**
     * @author Chaim
     * @date 2022/9/17 0:51
     */
    @Configuration
    public class MyCacheConfig {
    
        /**
         * 使 Redis 配置(spring.cache.redis)生效
         * 配置 Redis 自定义前缀 (可按需扩展)
         * 也可以通过: config.computePrefixWith(cacheName -> "redisProperties.getKeyPrefix()" + cacheName + ":"); 进行自定义配置, 本意是对函数式接口的实现
         *
         * @param cacheProperties Redis 配置参数
         * @return
         */
        @Bean
        public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
            RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
            //设置key用string类型保存,value用json格式保存
            config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
            config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(Object.class)));
    
            CacheProperties.Redis redisProperties = cacheProperties.getRedis();
            // 使配置文件中所有的配置都生效
            if (redisProperties.getTimeToLive() != null) {
                config = config.entryTtl(redisProperties.getTimeToLive());
            }
            if (redisProperties.getKeyPrefix() != null) {
                // 自定义前缀: spring.cache.redis.key-prefix
                // + 入参(org.springframework.data.redis.cache.CacheKeyPrefix.compute(String cacheName))
                // + value值 @Cacheable(value = "selectPage")
                config = config.computePrefixWith(cacheName -> redisProperties.getKeyPrefix() + cacheName + ":");
                // 前缀格式: spring.cache.redis.key-prefix + value值 @Cacheable(value = "selectPage")
                // config = config.prefixKeysWith(redisProperties.getKeyPrefix());
            }
            if (!redisProperties.isCacheNullValues()) {
                config = config.disableCachingNullValues();
            }
            if (!redisProperties.isUseKeyPrefix()) {
                // 写入 Redis 时是否使用前缀: org.springframework.data.redis.cache.RedisCache.createCacheKey
                config = config.disableKeyPrefix();
            }
    
            return config;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    效果图

    在这里插入图片描述



    在这里插入图片描述

  • 相关阅读:
    会议剪影 | 思腾合力受邀出席第四届长三角文博会并作主题演讲
    Excel 将数字添加百分号
    最火后台管理系统 RuoYi 项目探秘,之二
    C语言 100道经典编程题适用于专升本,专接本【详细分析版】
    华为数通方向HCIP-DataCom H12-821题库(单选题:501-520)
    STL函数库的讲解(1)
    F28069教程3-中断 PIE
    【云原生 | 从零开始学Kubernetes】十、k8sPod节点亲和性和反亲和性
    81. this、call、apply、bind?
    【HCIE】15.OSPFV3
  • 原文地址:https://blog.csdn.net/qq_38637558/article/details/127944666