首页 > 其他分享 >springboot 自定义整合caffeine 本地缓存

springboot 自定义整合caffeine 本地缓存

时间:2023-07-12 14:11:53浏览次数:36  
标签:pageCacheProperties 缓存 return springboot 自定义 caffeine private Integer public

1、自定义缓存配置类

@Data
@ConfigurationProperties(prefix = "page.cache")
public class PageCacheProperties {

    private CaffeineConfigProperties caffeine = new CaffeineConfigProperties(); //本地缓存配置
    private PageCacheAsyncExecutorConfig pool = new PageCacheAsyncExecutorConfig();

    private Integer checkJimDbCacheSecond = 200;   //jimdb缓存失效的最大时间 单位:秒
    private Integer flushIntervalSecond = 1;   // 如果缓存失效 间隔多少秒不重复刷新缓存  单位:秒
    private Integer jimDbExpireSecond = 300;   //jimDB缓存有效期 单位:秒

    @Data
    public class CaffeineConfigProperties {
        private Integer expireAfterWrite = 60; //单位秒  写入间隔多久淘汰
        private Integer maximumSize = 500;   //缓存 key 的最大个数
    }

    @Data
    public class PageCacheAsyncExecutorConfig {
        private Integer corePoolSize = 10; //核心线程数
        private Integer maxPoolSize = 20;   //最大线程数
        private Integer queueCapacity = 100;   //线程池 队列容量
    }

}

2、自定义缓存配置 yml

#页面缓存配置
page:
  cache:
    caffeine:
      expireAfterWrite: 60  #写入间隔多久淘汰 单位:秒
      maximumSize: 1000     #缓存 key 的最大个数
    checkJimDbCacheSecond: 240  #判断jimdb缓存失效的最大时间 单位:秒
    flushIntervalSecond: 1    #如果缓存失效 间隔多少秒不重复刷新缓存  单位:秒
    jimDbExpireSecond: 300   #页面缓存到jimdb的有效时间

 

3、加载缓存配置文件 并定义缓存管理器

@Slf4j
@Async
@Configuration
@EnableConfigurationProperties(PageCacheProperties.class)
public class PageCacheConfig implements AsyncConfigurer {

    @Autowired
    private PageCacheProperties pageCacheProperties;

    @Override
    @Bean("pageCacheTaskExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(pageCacheProperties.getPool().getCorePoolSize()); // 设置核心线程数
        executor.setMaxPoolSize(pageCacheProperties.getPool().getMaxPoolSize()); // 设置最大线程数
        executor.setQueueCapacity(pageCacheProperties.getPool().getQueueCapacity()); // 设置队列容量
        executor.setThreadNamePrefix("pageCacheTaskExecutor-"); // 设置线程名称前缀
        executor.initialize();
        return executor;
    }
    @Bean
    public CacheManager cacheManager(PageCacheProperties pageCacheProperties) {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(caffeineCacheBuilder(pageCacheProperties));
        return cacheManager;
    }

    private Caffeine<Object, Object> caffeineCacheBuilder(PageCacheProperties pageCacheProperties) {
        return Caffeine.newBuilder()
                .expireAfterWrite(pageCacheProperties.getCaffeine().getExpireAfterWrite(), TimeUnit.SECONDS)
                .maximumSize(pageCacheProperties.getCaffeine().getMaximumSize());
    }

    /**
     * 获取页面缓存的有效时间
     *
     * @return
     */
    public Integer getJimDbExpireSecond() {
        return pageCacheProperties.getJimDbExpireSecond();
    }

    /**
     * 获取 判断页面失效的 缓存时间
     *
     * @return
     */
    public Integer getCheckJimDbCacheSecond() {
        return pageCacheProperties.getCheckJimDbCacheSecond();
    }

    /**
     * 获取缓存失效 多少秒 不重复刷新缓存
     * @return
     */
    public Integer getFlushIntervalSecond(){
        return pageCacheProperties.getFlushIntervalSecond();
    }
}

4、本地缓存工具类

@Component
public class LocalCacheUtil {
    @Autowired
    private CacheManager cacheManager;

    /**
     * 获取缓存数据
     *
     * @param key
     * @return
     */
    public String getCacheData(String key) {
        Cache cache = cacheManager.getCache(key);
        return cache.get(key, String.class);
    }

    /**
     * 设置缓存数据
     *
     * @param key
     * @param value
     */
    public void setCacheData(String key, String value) {
        Cache cache = cacheManager.getCache(key);
        cache.put(key, value);
    }
}

 

5、异步执行

    /**
     * 异步查询pc缓存
     *
     * @param getPageAllDataParam
     */
    @Async("pageCacheTaskExecutor")
    public void asyncCheckAndUpdatePcCache(GetPageAllDataParam getPageAllDataParam) {

    }

 

标签:pageCacheProperties,缓存,return,springboot,自定义,caffeine,private,Integer,public
From: https://www.cnblogs.com/niun/p/17547335.html

相关文章

  • SpringBoot与Freemarker整合
    1.需要导入freemarker的pom文件;<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>2.需要在application.properties配置文件中配置一些freemarker的参数;serve.port......
  • vue3自定义指令 拖拽 与拖拽变大小
    directives:{drag:{mounted:(el,binding)=>{constdragDom=el;conststy=dragDom.currentStyle||window.getComputedStyle(dragDom,null);el.parentElement.style.cursor='move';......
  • 基于JavaFX的扫雷游戏实现(五)——设置和自定义控件
      它来了它来了,最后一期终于来了。理论上该讲的全都讲完了,只剩下那个拖了好几期的自定义控件和一个比较没有存在感的设置功能没有讲。所以这次就重点介绍它们俩吧。  首先我们快速浏览下设置的实现,上图:  然后是控制器代码:SettingsController.javapackagecontrollers;......
  • 二维码简易实现 Vue+Springboot
    Vue:<template><div><img:src="database64"width="150px"/><div>注:请使用手机微信扫码,并于2分钟内绑定员工账号(二维码为账号独属,请勿分享)。</div></div></template><script>import{getQrCode}from"......
  • 4-基于SpringBoot实现SSMP整合
    1.整合JunitSpring整合JUnit的制作方式//加载spring整合junit专用的类运行器@RunWith(SpringJUnit4ClassRunner.class)//指定对应的配置信息@ContextConfiguration(classes=SpringConfig.class)publicclassAccountServiceTestCase{//注入你要测试的对象......
  • React中编写操作树形数据的自定义Hook
    什么是Hookhook即为钩子,是一种特殊的函数,它可以让你在函数式组件中使用一些react特性,目前在react中常用的hook有以下几类useState:用于在函数组件中定义和使用状态(state)。useEffect:用于在函数组件中处理副作用,也可以模拟react生命周期useContext:用于在函......
  • SpringBoot 如何处理 CORS 跨域?
    Springboot跨域问题,是当前主流web开发人员都绕不开的难题。但我们首先要明确以下几点跨域只存在于浏览器端,不存在于安卓/ios/Node.js/python/java等其它环境跨域请求能发出去,服务端能收到请求并正常返回结果,只是结果被浏览器拦截了。之所以会跨域,是因为受到了同源策略的限......
  • SpringBoot 集成和使用 Dubbo
    Dubbo是阿里开源的产品,采用二进制通信,相比OpenFeign的http通信,具有性能优势,可以轻松集成到SpringBoot和SpringCloud中使用,对于性能要求比较高的场景,使用比较广泛。早期的Dubbo都采用Zookeeper作为注册中心,现在基本上大家都使用Nacos作为注册中心,毕竟Dubbo和Nac......
  • 基于java+springboot的宠物商店、宠物管理系统
    该系统是基于java+springboot开发的宠物商城,用户可以登录该网站购买宠物。该系统是给师弟开发的课程作业。运行过程中的问题,可以咨询github或留言。演示地址前台地址:http://pet.gitapp.cn后台地址:http://pet.gitapp.cn/admin后台管理帐号:用户名:admin123密码:admin123源......
  • 基于java+springboot的旅游信息网站、旅游景区门票管理系统
    该系统是基于java+springboot开发的旅游景区门票管理系统。是给师弟开发的大四实习作品。学习过程中,遇到问题可以咨询github作者。演示地址前台地址:http://travel.gitapp.cn后台地址:http://travel.gitapp.cn/admin后台管理帐号:用户名:admin123密码:admin123源码地址htt......