Caffeine 缓存
Caffeine 内存缓存使用笔记
SpringBoot 1.x 版本中的默认本地 cache 是 Guava Cache
Spring Boot 2.x(Spring5) 开始,Spring 官方放弃了 Guava 转而使用更优秀的 Caffeine 作为默认缓存组件。
建议结合 Spring Cache 的 @Cacheable
等注解一起使用。
Caffeine 提供了三种定时驱逐策略:expireAfterAccess(long, TimeUnit)
在最后一次访问或者写入后开始计时,在指定的时间后过期。假如一直有请求访问该key,那么这个缓存将一直不会过期。expireAfterWrite(long, TimeUnit)
在最后一次写入缓存后开始计时,在指定的时间后过期。expireAfter(Expiry)
自定义策略,过期时间由 Expiry 实现独自计算。
缓存的删除策略使用的是惰性删除和定时删除。这两个删除策略的时间复杂度都是O(1)。
配置类
com.github.benmanes.caffeine.cache.CaffeineSpec
void configure(String key, @Nullable String value) {
switch (key) {
case "initialCapacity":
this.initialCapacity(key, value);
return;
case "maximumSize":
this.maximumSize(key, value);
return;
case "maximumWeight":
this.maximumWeight(key, value);
return;
case "weakKeys":
this.weakKeys(value);
return;
case "weakValues":
this.valueStrength(key, value, Strength.WEAK);
return;
case "softValues":
this.valueStrength(key, value, Strength.SOFT);
return;
case "expireAfterAccess":
this.expireAfterAccess(key, value);
return;
case "expireAfterWrite":
this.expireAfterWrite(key, value);
return;
case "refreshAfterWrite":
this.refreshAfterWrite(key, value);
return;
case "recordStats":
this.recordStats(value);
return;
default:
throw new IllegalArgumentException("Unknown key " + key);
}
}
上一篇 Spring-Event
下一篇 Spring-Utils
页面信息
location:
protocol
: host
: hostname
: origin
: pathname
: href
: document:
referrer
: navigator:
platform
: userAgent
: