前面的文章缓存篇(一)- Guava <https://blog.csdn.net/codingtu/article/details/81298229>
有讲到Guava
Cache有区别于ConcurrentHashMap的使用,就是因为其自带有自动刷新和自动失效的功能,避免我们去自己编写刷新和失效的后台线程程序。Guava
Cache提供了简单便捷的api给我们使用,但是研究源码发现这里的自动刷新缓存和自动失效原理,并非是Guava
Cache帮我们去添加了类似后台线程自动刷新或失效逻辑的代码,而是用另外一种巧妙的方式进行。

<>expireAfterWrite 与 refreshAfterWrite区别

<>expireAfterWrite


在缓存更新后某个时间失效缓存,这里Guava内部会对某个时间点失效的缓存做统一失效,只要有get访问任一key,就会失效当前时间失效的缓存,会移除当前key。所以这里也希望我们创建的缓存数据量不宜过大,使用guavaCache最好是设置一下maximumSize,避免出现内存溢出的情况。失效后需要获取新值才可会返回。

<>refreshAfterWrite


是指在创建缓存后,如果经过一定时间没有更新或覆盖,则会在下一次获取该值的时候,会在后台异步去刷新缓存,如果新的缓存值还没有load到时,则会先返回旧值。这里跟上面的expireAfterWrite不同的是,及时到了该刷新的时间,不会失效旧值和移除对应key。在后台异步刷新的过程中,如果当前是刷新状态,及时有其他线程访问到旧值,依然只有一个线程在更新,不会出现多个线程同时刷新同一个key的缓存。

<>是否需要编写缓存刷新代码


不需要。上面讲到,使用了refreshAfterWrite后台会异步去刷新。这里后台刷新是使用线程池去完成异步刷新过程,即ListeningExecutorService
sameThreadExecutor = MoreExecutors.sameThreadExecutor();

<>测试代码

这里是模拟1000个线程在3秒内10次从guavaCacha拿数据,通过loadTimes判断刷新了多少次,输出结果可以看到拿到的结果正确性。

代码已上传至https://github.com/zhuzhenke/common-caches
<https://github.com/zhuzhenke/common-caches/blob/ed171a68108a37e4f9cbb8e5fccea7bc12235896/google-guava/src/test/java/com/google/guava/cache/GuavaCacheRefreshTest.java>
public class GuavaCacheRefreshTest { @Data public class SkuCache { private
String skuId; private String skuCode; private Long realQuantity; }
AtomicInteger loadTimes= new AtomicInteger(0); AtomicInteger count = new
AtomicInteger(0); @Test public void testCacheUse() throws Exception {
LoadingCache<String, SkuCache> loadingCache = CacheBuilder.newBuilder() .
refreshAfterWrite(1000, TimeUnit.MILLISECONDS) //Prevent data reloading from
failing, but the value of memory remains the same .expireAfterWrite(1500,
TimeUnit.MILLISECONDS) .build(new CacheLoader<String, SkuCache>() { @Override
public SkuCache load(String key) { SkuCache skuCache = new SkuCache(); skuCache.
setSkuCode(key + "---" + (loadTimes.incrementAndGet())); skuCache.setSkuId(key);
skuCache.setRealQuantity(100L); System.out.println("load..." + key); return
skuCache; } @Override public ListenableFuture<SkuCache> reload(String key,
SkuCache oldValue) throws Exception { checkNotNull(key); checkNotNull(oldValue);
System.out.println("reload..."); //Simulate time consuming operation //
Thread.sleep(1000); return Futures.immediateFuture(load(key)); } }); for (int i
= 0; i < 1000; i++) { new Thread(() -> { try { getValue(loadingCache); } catch (
Exception e) { e.printStackTrace(); } }).start(); } System.in.read(); System.out
.println("finish"); } private void getValue(LoadingCache<String, SkuCache>
loadingCache) throws Exception { for (int i = 0; i < 10; i++) { Thread.sleep(300
l); System.out.println(loadingCache.get("sku").toString() + " - " + count.
incrementAndGet()); } } }

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信