说起本地缓存,很多人可能会想到guava,guava是google一些列程序包中一个非常方便好用的本地化缓存实现。在以往项目中有需要本地缓存方案时,我都会用guava来做实现,确实很有效且方便。但前段时间看到Spring官方推荐的本地化缓存实现方案为caffeine,很吃惊不是guava,考虑到caffeine是Spring官方推荐的,应该是有其独到之处。
性能
从上图可看出caffeine的性能是非常出色的,具体可参见官方文档caffeine官方文档
入手实践
- 1、首先在项目中引入jar包:
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.7.0</version>
</dependency>
- 2、创建缓存对象,开始使用
private Cache<Long,String> caffeineCache = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.maximumSize(20)
.initialCapacity(4)
.build();
- 2.1、参数含义
- initialCapacity: 初始的缓存空间大小;
- maximumSize: 缓存的最大数量;
- maximumWeight: 缓存的最大权重;
- expireAfterAccess: 最后一次访问(读/写)操作后经过指定时间过期;
- expireAfterWrite: 最后一次写操作后经过指定时间过期;
- refreshAfterWrite: 创建缓存或者最近一次更新缓存后经过指定时间间隔,刷新缓存;
- weakKeys: 打开key的弱引用;
- weakValues:打开value的弱引用;
- softValues:打开value的软引用;
- recordStats:开发统计功能;
- 3.1、自行判断添加缓存对象
// 自行判断添加缓存
public String method1(Long key) {
if (null == caffeineCache.getIfPresent(key)) {
String value = getValueByKey(key);
caffeineCache.put(key, value);
return value;
}
return caffeineCache.getIfPresent(key);
}
private String getValueByKey(Long key) {
return "caffeine cache value " + key;
}
- 3.2、自动判断添加缓存对象
// 自动判断添加缓存 程序在获取不到value值时,会调用function函数自行添加缓存
public String method2(Long key) {
return caffeineCache.get(key, new Function<Long, String>() {
@Override
public String apply(Long aLong) {
return getValueByKey(aLong);
}
});
}
- 4、潜在的问题
- 在分布式环境下,使用本地缓存有可能会造成数据不一致现象;
- 本地缓存会消耗内存,不宜使用太多,否则容易造成内存溢出;