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 List<String> cacheName;
private Duration expireAfterWrite;
private Duration expireAfterAccess;
private Long maximumSize = Long.valueOf(-1);
private List<Config> configs;
@Getter
@Setter
public static class Config {
private List<String> 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();
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。










