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