说明
在上篇博文中,我使用了guava作为spring的本地缓存,来实现在加载数据到本地的需求。在这个篇博文中,我将记录总结使用caffeine作为本地缓存。在spring5后,spring官方放弃了guava,而使用了性能更优秀的caffeine,本篇将结合springboot2.x来实现caffeine的使用。了解有关caffeine的详细内容请见caffeine。
正文
1.CacheProperties
同样,本篇也采用的是手动配置参数类,没有使用springboot的自动配置。首先是创建配置属性类CacheProperties。
@ConfigurationProperties(prefix = "spring.cache")
public class CacheProperties {
private int initialCapacity; //初始缓存空间大小
private long maximnumSize; //缓存的最大条数
private long maximunmWeight; //缓存的最大权重
private long expireAfterAccess; //最后一次写入或访问后经过固定的时间过期
private long expireAfterWrite; //最后一次写入后经过固定的时间过期
private long refreshAfterWrite; //写入后经过固定的时间刷新缓存
private boolean weakKeys; // key为弱引用
private boolean weakValues; // value为弱引用
private boolean softValues; //value 为软引用
private boolean recordStats; //统计功能
....//省