1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
</dependency>
如果引入spring依赖不够,使用注解启动报错:
java.lang.ClassNotFoundException: org.aspectj.util.PartialOrder$PartialComparable
,可以引入依赖:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
<scope>runtime</scope>
</dependency>
具体实现:
@Configuration
//开启缓存
@EnableCaching
public class CaffeineConfig {
/**
* 创建基于Caffeine的Cache Manager
* @return
*/
@Bean
@Primary
public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
ArrayList<CaffeineCache> caches = new ArrayList<CaffeineCache>();
for(CachesEnum c : CachesEnum.values()){
caches.add(new CaffeineCache(c.name(),
Caffeine.newBuilder().recordStats()
.expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
.maximumSize(c.getMaxSize())
.build())
);
}
cacheManager.setCaches(caches);
return cacheManager;
}
}
/**
* @Description: 款存枚举设置;枚举的name(),就是实际款存@Cacheable里面的值
* 例如:CODE_MAP_MAS_CACHE(30,1000,TimeUnit.SECONDS)的name()是CODE_MAP_MAS_CACHE,
* 它对应的就是@Cacheable(value ="CODE_MAP_MAS_CACHE")
*/
public enum CachesEnum {
//有效期30秒,最大容量1000,单位秒
CODE_MAP_MAS_CACHE(30,1000,TimeUnit.SECONDS),
;
CachesEnum() {
}
CachesEnum(int ttl) {
this.ttl = ttl;
}
CachesEnum(int ttl, int maxSize,TimeUnit timeUnit) {
this.ttl = ttl;
this.maxSize = maxSize;
this.maxSize = maxSize;
}
/** 最大數量*/
private int maxSize= 10000;
/** 过期时间(秒)*/
private int ttl= 600;
/** 时间单位*/
private TimeUnit timeUnit= TimeUnit.SECONDS;;
public int getMaxSize() {
return maxSize;
}
public int getTtl() {
return ttl;
}
public TimeUnit getTimeUnit() {
return timeUnit;
}
@Override
public String toString() {
return "CachesEnum{" +
"maxSize=" + maxSize +
", ttl=" + ttl +
", timeUnit=" + timeUnit +
'}';
}
}
@Component
public class CodeMapMasCache {
private static final Logger logger = LoggerFactory.getLogger(CodeMapMasCache.class);
@Autowired
private CodeMapMapper codeMapMapper;
/** 命名规范,表名+ CACHE */
@Cacheable(value ="CODE_MAP_MAS_CACHE")
public Map<String,CodeMapMasDTO> getCodeMapMasCache(){
logger.debug(this.getClass().getSimpleName()+":本地缓存{"+"key"+"},正在从数据库中获取CodeMapMasDTO!获取时间:"+new Date());
List<CodeMapMasDTO> codeMapInfoList = null;
Map<String, CodeMapMasDTO> resultMap = new HashMap<String, CodeMapMasDTO>();
try {
codeMapInfoList = codeMapMapper.getAllCodeMapMasCache();
for (CodeMapMasDTO codeMapMasDTO : codeMapInfoList) {
resultMap.put(codeMapMasDTO.getCodeId(), codeMapMasDTO);
}
} catch (Exception e) {
logger.error(this.getClass().getSimpleName() + ":===codeMapMapper.getAllCodeMapMasCache()===查询基础参数信息失败:", e);
}
return resultMap;
}
}
public interface CaffeineCacheService {
public CodeMapMasDTO getCodeMapMasCacheSingle(String codeId) ;
public String getCodeMapMasCacheAttr1(String codeId);
public String getCodeMapMasCacheAttr2(String codeId);
public String getCodeMapMasCacheAttr3(String codeId);
public String getCodeMapMasCacheAttr4(String codeId);
}
@Component("guavaCacheService")
public class CaffeineCacheServiceImpl implements CaffeineCacheService {
@Autowired(required = false)
private CodeMapMasCache codeMapMasCache;
@Autowired(required = false)
private CodeMapMapper codeMapMapper;
public Map<String, CodeMapMasDTO> getCodeMapMasCache() {
Map<String, CodeMapMasDTO> codeMap = null;
codeMap = codeMapMasCache.getCodeMapMasCache();
return codeMap;
}
@Override
public CodeMapMasDTO getCodeMapMasCacheSingle(String codeId) {
CodeMapMasDTO codeMapMasDTO = null;
Map<String, CodeMapMasDTO> map = this.getCodeMapMasCache();
if (!CollectionUtils.isEmpty(map)) {
codeMapMasDTO = map.get(codeId);
}
return codeMapMasDTO;
}
@Override
public String getCodeMapMasCacheAttr1(String codeId) {
CodeMapMasDTO codeMapMasDTO = this.getCodeMapMasCacheSingle(codeId);
if (codeMapMasDTO != null) {
return codeMapMasDTO.getAttribute1();
}
return null;
}
@Override
public String getCodeMapMasCacheAttr2(String codeId) {
CodeMapMasDTO codeMapMasDTO = this.getCodeMapMasCacheSingle(codeId);
if (codeMapMasDTO != null) {
return codeMapMasDTO.getAttribute2();
}
return null;
}
@Override
public String getCodeMapMasCacheAttr3(String codeId) {
CodeMapMasDTO codeMapMasDTO = this.getCodeMapMasCacheSingle(codeId);
if (codeMapMasDTO != null) {
return codeMapMasDTO.getAttribute3();
}
return null;
}
@Override
public String getCodeMapMasCacheAttr4(String codeId) {
CodeMapMasDTO codeMapMasDTO = this.getCodeMapMasCacheSingle(codeId);
if (codeMapMasDTO != null) {
return codeMapMasDTO.getAttribute4();
}
return null;
}
}
测试:
@RestController
public class TestCache {
@Autowired
CaffeineCacheService guavaCacheService;
@RequestMapping(value ="/cache",method = RequestMethod.POST)
public Result transportation( @RequestParam String codeId){
String attr1 = guavaCacheService.getCodeMapMasCacheAttr1(codeId);
return new Result("1","请求成功",attr1);
}
}
参考:
https://blog.csdn.net/zhuyu19911016520/article/details/81946202
https://blog.csdn.net/sinat_36553913/article/details/86417114