一、Caffine是什么?
Caffeine是一个基于Java 8的[高性能](https://github.com/ben-manes/caffeine/wiki/Benchmarks),[接近最佳的](https://github.com/ben-manes/caffeine/wiki/Efficiency)缓存库
Caffine有什么特色?
-
到缓存中,可选择异步
-
基于超过最大值时的
-
自上次访问或上次写入以来测量的条目
-
当第一个条目的陈旧请求发生时,
-
密钥自动包含在
-
值自动包含在
-
被驱逐(或以其他方式删除)条目的
-
到外部资源
-
累积缓存访问
二、Caffine缓存的使用
加入依赖
<dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.6.2</version> </dependency>
简单使用
com.github.benmanes.caffeine.cache.LoadingCache<Integer,List<TCoupon>> couponCaffeine = Caffeine.newBuilder() .expireAfterWrite(10,TimeUnit.MINUTES).refreshAfterWrite(5,TimeUnit.MINUTES) .build(new com.github.benmanes.caffeine.cache.CacheLoader<Integer, List<TCoupon>>() { @Override public List<TCoupon> load(Integer o) throws Exception { return loadCoupon(o); } }); /*** * 获取有效时间的可用优惠券列表 * // 1、是否存在远程调用 HTTP、RPC Metrics * // 2、大量内存处理 list.contain() ==>set.contain * @return */ public List<TCoupon> getCouponListCaffeine(){ List<TCoupon> tCoupons = Lists.newArrayList(); try { tCoupons = couponCaffeine.get(1); } catch (Exception e) { e.printStackTrace(); } return tCoupons; }
本文作者为DBC,转载请注明。