最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Spring Boot Cache使用方法整合代码实例
时间:2022-06-29 00:57:44 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Spring Boot Cache使用方法整合代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
使用本地Caffeine缓存
引入依赖包
org.springframework.boot spring-boot-starter-cache com.github.ben-manes.caffeine caffeine 2.6.2
自定义Caffeine配置
CachingConfig.java
package com.vcredit.vmp.checkcenter.config; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.vcredit.vmp.checkcenter.common.properties.CaffeineCacheProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.caffeine.CaffeineCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import java.time.Duration; import java.util.*; /** * 缓存配置 * @author kancy */ @Configuration @EnableCaching public class CachingConfig { @Autowired CaffeineCacheProperties caffeineCacheProperties; /** * 创建基于Caffeine的Cache Manager * @return */ @Bean @Primary @ConditionalOnProperty(prefix = "system.cache.caffeine" , name = "enabled", havingValue = "true") public CacheManager caffeineCacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); MapcacheMap = new HashMap(); // 设置全局配置的本地缓存 List globalCacheNames = caffeineCacheProperties.getCacheName(); if(globalCacheNames !=null && !globalCacheNames.isEmpty()){ addCacheObject(cacheMap, globalCacheNames, caffeineCacheProperties.getExpireAfterWrite(), caffeineCacheProperties.getExpireAfterAccess(), caffeineCacheProperties.getMaximumSize()); } // 设置自定义属性缓存, 可以覆盖全局缓存 List configs = caffeineCacheProperties.getConfigs(); if(configs != null && !configs.isEmpty()){ for (CaffeineCacheProperties.Config config : configs) { List cacheNames = config.getCacheName(); if (cacheNames == null || cacheNames.isEmpty()){ continue; } Duration expireAfterWrite = Optional.ofNullable(config.getExpireAfterWrite()).orElse(caffeineCacheProperties.getExpireAfterWrite()); Duration expireAfterAccess = Optional.ofNullable(config.getExpireAfterAccess()).orElse(caffeineCacheProperties.getExpireAfterAccess()); Long maximumSize = Optional.ofNullable(config.getMaximumSize()).orElse(caffeineCacheProperties.getMaximumSize()); addCacheObject(cacheMap, cacheNames, expireAfterWrite, expireAfterAccess, maximumSize); } } // 加入到缓存管理器进行管理 cacheManager.setCaches(cacheMap.values()); return cacheManager; } private void addCacheObject(Map cacheMap, List cacheNames, Duration expireAfterWrite, Duration expireAfterAccess, Long maximumSize) { for (String cacheName : cacheNames) { // spring.cache.caffeine: maximumSize=500,expireAfterAccess=10s,expireAfterWrite=15s Caffeine
CaffeineCacheProperties.java
package com.vcredit.vmp.checkcenter.common.properties; import lombok.Getter; import lombok.Setter; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.time.Duration; import java.util.List; /** * Caffeine本地缓存自定义配置 * @author kancy */ @Getter @Setter @Configuration @ConfigurationProperties("system.cache.caffeine") @ConditionalOnProperty(prefix = "system.cache.caffeine" , name = "enabled", havingValue = "true") public class CaffeineCacheProperties { private ListcacheName; private Duration expireAfterWrite; private Duration expireAfterAccess; private Long maximumSize = Long.valueOf(-1); private List configs; @Getter @Setter public static class Config { private List cacheName; Duration expireAfterWrite; Duration expireAfterAccess; Long maximumSize; } }
application.yml
system.cache.caffeine: enabled: true # 全局配置 cacheName: cache1,cache2,cache3 expireAfterWrite: 60s expireAfterAccess: 30s maximumSize: 500 # 自定义配置,cacheName相同可覆盖全局 configs: - cacheName: checkApplyCache expireAfterAccess: 10s - cacheName: userQueryCache expireAfterAccess: 15s
使用缓存
@Cacheable(value = { "checkApplyCache" }, key="#req.md5") public Result check(CheckReq req) { // your code... return Result.ok(); }
相关文章
- 《无限暖暖》织衣蛛获得方法 12-26
- 《无限暖暖》美食家蜜蜂获得方法 12-26
- 《无限暖暖》泳镜蝽获得方法 12-26
- 《无限暖暖》芭蕾豆娘获得方法 12-26
- 《无限暖暖》刺绣蛾获得方法 12-26
- 《无限暖暖》尖帽仙获得方法 12-26