caffeine
Caffeine 是一个 Java 缓存库,它提供了一种简单而强大的方式来管理缓存数据
1.学习文档
https://github.com/ben-manes/caffeine/wiki/Home-zh-CN
https://www.cnblogs.com/crazymakercircle/p/14385641.html
2.依赖
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>
3.基础样例 Java8
@Slf4j
public class caffeine {
/**
* 产品模型缓存,一小时过期
*/
private static final LoadingCache<String, JSONObject> deviceModelCache =
Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS)
.build(caffeine::getDeviceModel);
private static JSONObject getDeviceModel(String key){
JSONObject object =new JSONObject();
object.put(key,key+"model");
return object;
}
public static void main(String[] args) {
log.info(deviceModelCache.get("w601").toJSONString());
log.info(deviceModelCache.get("m100").toJSONString());
}
}