- 配置
@Configuration
@EnableCaching
public class CacheConfigration {
private static final int INITIAL_CAPACITY = 1000;
private static final int MAX_IMUM_SIZE = 10000;
@Bean("caffeineCacheManager")
@Primary
public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<CaffeineCache> caffeineCaches = new ArrayList<>();
for (CacheTypeEnum cacheType : CacheTypeEnum.values()) {
caffeineCaches.add(new CaffeineCache(cacheType.name(),
Caffeine.newBuilder()
.initialCapacity(INITIAL_CAPACITY) //初始大小
.maximumSize(MAX_IMUM_SIZE) //最大大小
.expireAfterWrite(cacheType.getExpires(), cacheType.getUnit())
.build()));
}
cacheManager.setCaches(caffeineCaches);
return cacheManager;
}
@Bean
public CacheLoader<String, Object> cacheLoader() {
return new CacheLoader<String, Object>() {
@Override
public Object load(String key) throws Exception {
return null;
}
// 重写这个方法将oldValue值返回回去,进而刷新缓存
@Override
public Object reload(String key, Object oldValue) throws Exception {
return oldValue;
}
};
}
}
枚举值
public enum CacheTypeEnum {
// 缓存90天
NINETY_DAYS(90, TimeUnit.DAYS),
// 缓存30天
THIRTY_DAYS(30, TimeUnit.DAYS),
// 30分钟
THIRTY_MINUTES(30, TimeUnit.MINUTES),
//缓存1小时
ONE_HOURS(1, TimeUnit.HOURS),
//缓存2小时
TWO_HOURS(2, TimeUnit.HOURS),
//缓存1分钟
ONE_MINUTES(1, TimeUnit.MINUTES),
//缓存10分钟
TEN_MINUTES(10, TimeUnit.MINUTES),
//6个小时
SIX_HOURS(6, TimeUnit.HOURS);
private int expires;
private TimeUnit unit;
CacheTypeEnum(int expires, TimeUnit unit) {
this.expires = expires;
this.unit = unit;
}
public int getExpires() {
return expires;
}
public TimeUnit getUnit() {
return unit;
}
public static String getCacheName(int expires, TimeUnit unit) {
if (null == unit) {
Asserts.fail("缓存时间单位不能为空");
}
for (CacheTypeEnum enums : values()) {
if (expires == enums.getExpires() && unit.name().equals(enums.getUnit().name())) {
return enums.name();
}
}
Asserts.fail("未配置对应缓存");
return null;
}
- 启动类添加@EnableCaching注解
- 工具类
@Component
public class CacheUtils<K, V> {
@Resource
private CacheManager caffeineCacheManager;
/**
* 设置指定时间内过期的key
*
* @param key
* @param value
*/
public void setEx(K key, V value, String cacheName) {
Cache cache = caffeineCacheManager.getCache(cacheName);
if (null == cache) {
Asserts.fail("未配置缓存信息");
}
cache.put(key, value);
}
/**
* 根据key获取相应的值
*
* @param key
* @return
*/
public V get(K key, String cacheName) {
Cache cache = caffeineCacheManager.getCache(cacheName);
if (null == cache) {
Asserts.fail("未配置缓存信息");
}
Cache.ValueWrapper valueWrapper = cache.get(key);
if (null == valueWrapper) {
return null;
}
return (V) valueWrapper.get();
}
/**
* 移除指定的key
*
* @param key
*/
public void remove(K key, String cacheName) {
Cache cache = caffeineCacheManager.getCache(cacheName);
if (null == cache) {
Asserts.fail("未配置缓存信息");
}
cache.evict(key);
}
}
- 使用方法
1. 注解: @Cacheable(cacheNames = "枚举名", key = RedisKeyConst.AREA_TREE_KEY + "+#blockNoStationTownship")
2. 工具类
cacheUtils.setEx(RedisKeyConst.DICTIONARY_ID_KEY + dict.getCode() + CommonSymbolConstant.UNDERLINE + dict.getValue(),
dict.getDictDesc(), CacheTypeEnum.THIRTY_DAYS.name());