• spring cache 的常规使用


    spring cache

    spring 提供了cache的封装

    需要引入:

     <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-cacheartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
                <version>2.3.9.RELEASEversion>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    根据项目需要 可以设置cache 的具体实现,有如下类型

    public enum CacheType {
    
       /**
        * Generic caching using 'Cache' beans from the context.
        */
       GENERIC,
    
       /**
        * JCache (JSR-107) backed caching.
        */
       JCACHE,
    
       /**
        * EhCache backed caching.
        */
       EHCACHE,
    
       /**
        * Hazelcast backed caching.
        */
       HAZELCAST,
    
       /**
        * Infinispan backed caching.
        */
       INFINISPAN,
    
       /**
        * Couchbase backed caching.
        */
       COUCHBASE,
    
       /**
        * Redis backed caching.
        */
       REDIS,
    
       /**
        * Caffeine backed caching.
        */
       CAFFEINE,
    
       /**
        * Simple in-memory caching.
        */
       SIMPLE,
    
       /**
        * No caching.
        */
       NONE
    
    }
    
    • 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

    学习掌握的重点集中在redis 和CAFFEINE

    redis 是分布式系统必不可少的缓存上实现方式,spring cache 的封装逻辑可以满足大部分如

    配置文件、数据字典的缓存逻辑, 避免 数据库访问,或者程序直接的调用

    redis的重点是提供分布式的缓存 机制,缓存超时,但是内存回收则是redis 服务自己控制
    caffeine 组件则是提供了更加丰富的内存回收控制机制。

    spring cache redis 实现

    网上大部分资料都需要自己实现

    CacheManager

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory lettuceConnectionFactory) {
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
        // 设置缓存管理器管理的缓存的默认过期时间
        defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(defaultExpireTime))
                // 设置 key为string序列化
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                // 设置value为json序列化
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
                // 不缓存空值
                .disableCachingNullValues();
    
        Set<String> cacheNames = new HashSet<>();
        cacheNames.add(userCacheName);
    
        // 对每个缓存空间应用不同的配置
        Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
        configMap.put(userCacheName, defaultCacheConfig.entryTtl(Duration.ofSeconds(userCacheExpireTime)));
        configMap.put("users", defaultCacheConfig.entryTtl(Duration.ofSeconds(400)));
    
        RedisCacheManager cacheManager = RedisCacheManager.builder(lettuceConnectionFactory)
                .cacheDefaults(defaultCacheConfig)
                .initialCacheNames(cacheNames)
                .withInitialCacheConfigurations(configMap)
                .build();
        return cacheManager;
    }
    
    • 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

    KeyGenerator

    @Bean
    public KeyGenerator keyGenerator() {
        /**
         * target: 类
         * method: 方法
         * params: 方法参数
         */
        return (target, method, params) -> {
            //获取代理对象的最终目标对象
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getSimpleName()).append(":");
            sb.append(method.getName()).append(":");
            //调用SimpleKey的key生成器
            Object key = SimpleKeyGenerator.generateKey(params);
            return sb.append(key);
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    另外有人开源了自定义的

    public interface Cache
    
    • 1

    逻辑,实现了一级缓存使用 caffeine 在服务的内存中,二级缓存在redis 中共享,平衡了内存资源的使用

    参考源码:https://github.com/xiaolyuh/layering-cache

    反例是一种在service 中实现的缓存逻辑

    反例可以实现功能但是 封装的不够优雅,代码复用性不佳

    https://blog.csdn.net/Weixiaohuai/article/details/121314530

    配置文件

    配置文件:

    spring:
      cache:
        redis:
          key-prefix: "sp:"
          time-to-live: 800
        type: redis
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意需要增加一个注解 才能使配置文件生效

    @CacheConfig(cacheNames = "users")
    public class UserService {....}
    
    • 1
    • 2

    参考文章:https://www.fancv.com/article/1663491724

  • 相关阅读:
    分布式 - 公司使用什么RPC框架,聊聊你理解的RPC原理
    计算机专业大学生应该怎么规划未来?
    Modbus转Profinet网关在污水处理系统中连接PLC和变频器Modbus通信案例
    python3,设置只读属性
    java的file类的常用的操作实战分享来啦
    Web APIs:事件高级--注册事件(绑定事件)
    Elasticsearch中的语义检索
    2022人工智能顶会时间序列论文汇总
    注解【元数据,自定义注解等概念详解】(超简单的好吧)
    POL890 LVDS&RGB TO MIPI&LVDS
  • 原文地址:https://blog.csdn.net/keep_learn/article/details/126919937