• springboot集合caffeine实现本地缓存(模板,可直接cv)


    CacheConfig 配置

    要在项目中使用下本地缓存,方便更快的调用数据, 查询了很多文档发现都比较冗余,于是自己摸索了好一会才整理出一套能用的模板。文末附上了完整代码

    1. 引入pom.xml
    		<dependency>
                <groupId>com.github.ben-manes.caffeine</groupId>
                <artifactId>caffeine</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    1. 定义一个缓存key生成的规则,后续被cacheable调用。
     /**
         * 自定义的key生成规则.
         * @return
         */
        @Bean("dataKeyGenerator")
        public KeyGenerator dataKeyGenerator() {
            return (o, method, objects) -> method.getName() + "_" + Arrays.asList(objects);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 定义cacheManager管理缓存
     @Bean
        public CacheManager cacheManager(Ticker ticker) {
            // 缓存的数据,有效期60分钟,可以一次性配置多个
            CaffeineCache dataClustersCache = buildCache("data_types_clusters", ticker, 60);
            CaffeineCache dataHostCache = buildCache("data_types_hosts", ticker, 60);
    
            SimpleCacheManager manager = new SimpleCacheManager();
            // 一起放入
            manager.setCaches(Arrays.asList(dataClustersCache, dataHostCache));
            return manager;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 初始化CaffeineCache配置,只需要关注几个属性

    expireAfterWrite 最后一次写入后经过固定时间过期
    expireAfterAccess 最后一次写入或访问后经过固定时间过期
    refreshAfterWrite 创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存
    一般使用用expireAfterWrite 即可

    private CaffeineCache buildCache(String name, Ticker ticker, int minutesToExpire) {
            return new CaffeineCache(name, Caffeine.newBuilder()
                    .expireAfterWrite(minutesToExpire, TimeUnit.MINUTES)
                    .maximumSize(100)
                    .ticker(ticker)
                    .build());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 比较简单,直接上完整代码
    
    import com.github.benmanes.caffeine.cache.Caffeine;
    import com.github.benmanes.caffeine.cache.Ticker;
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.caffeine.CaffeineCache;
    import org.springframework.cache.interceptor.KeyGenerator;
    import org.springframework.cache.support.SimpleCacheManager;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.Arrays;
    import java.util.concurrent.TimeUnit;
    
    /** springboot的caffeine本地缓存配置.
     */
    @Slf4j
    @Configuration
    public class CacheConfig {
    
        @Data
        public static class CacheSpec {
            private Integer timeout;
            private Integer max = 200;
        }
    
        @Bean
        public CacheManager cacheManager(Ticker ticker) {
            // 下拉框的数据,有效期60分钟
            CaffeineCache dataTest1 = buildCache("data_types_test1", ticker, 60);
            CaffeineCache dataTest2 = buildCache("data_types_test2", ticker, 60);
    
            SimpleCacheManager manager = new SimpleCacheManager();
            manager.setCaches(Arrays.asList(dataTest1 , dataTest2 ));
            return manager;
        }
    
        private CaffeineCache buildCache(String name, Ticker ticker, int minutesToExpire) {
            return new CaffeineCache(name, Caffeine.newBuilder()
                    .expireAfterWrite(minutesToExpire, TimeUnit.MINUTES)
                    .maximumSize(100)
                    .ticker(ticker)
                    .build());
        }
    
        /**
         * 自定义的key生成规则.
         * @return
         */
        @Bean("dataKeyGenerator")
        public KeyGenerator dataKeyGenerator() {
            return (o, method, objects) -> method.getName() + "_" + Arrays.asList(objects);
        }
    
        @Bean
        public Ticker ticker() {
            return Ticker.systemTicker();
        }
    }
    
    
    • 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
    • 58
    • 59
    • 60
    • 61

    使用的时候只需要把cacheManager中自己需要的key,写入即可

    @EnableCaching

    启动方法类上添加注解@EnableCaching

    具体使用

    	@Override
        @Cacheable(cacheNames = "data_types_test1", keyGenerator = "dataKeyGenerator")
        public List<String> test() {
            return xxx;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    CV后要修改的其实就是一个manager,以及自己要使用的方法上添加下对应的cacheable注解即可。

    注意一个问题,controller调用service的test()会调用缓存,但是如果是service内部间的调用test(), 并不会触发缓存。目前也不知道原因。

  • 相关阅读:
    基于随机森林实现特征选择降维及回归预测(Matlab代码实现)
    WorkManager学习一
    从安装python到使用opencv进行人脸检测
    Java InputStream 中文乱码
    SSK:超级键盘模拟器,调用底层,可模拟所有按键
    C/C++---------------LeetCode第1394.找出数组中的幸运数
    数字时代的自我呈现:探索个人形象打造的创新工具——FaceChain深度学习模型工具
    luckysheet的一点使用心得
    关于数据权限的设计
    2022-10-28 linux IO指令 读写GPIO口电平实例
  • 原文地址:https://blog.csdn.net/ideaxx/article/details/133991029