A comparison of local caches (2) 【本地缓存之比较 (2)】
接上一篇: A comparison of local caches (1) 【本地缓存之比较 (1)】
This article will compare the asynchronous local caches.
Currently Spring @Cacheable doesn't support async cache by default (we can use some tricks to achieve the goal though).
Guava and Caffiene support async cache with refresh method.
本文会对异步刷新的本地缓存做个介绍。
目前 Spring 的 @Cacheable 注解默认不支持异步刷新(可以使用一些特殊的技巧来实现,这里暂时不提)
Guava 和 Caffiene 的 refresh 方法都对缓存的异步刷新提供了很好的支持。
1. Guava
public class GuavaAsyncCacheExample {
static AtomicInteger counter = new AtomicInteger(0);
static ListeningExecutorService exe = MoreExecutors.listeningDecorator(Executors.newWorkStealingPool());
static LoadingCache<Integer, Integer> refreshCache = CacheBuilder.newBuilder()
.refreshAfterWrite(3, TimeUnit.SECONDS)
.recordStats()
.build(new CacheLoader<Integer, Integer>() {
@Override
public Integer load(Integer key) throws Exception {
return getIntegerCache();
}
public ListenableFuture<Integer> reload(Integer key, Integer oldValue) throws Exception {
return exe.submit(() -> getIntegerCache());
}
});
private static Integer getIntegerCache() throws Exception {
System.out.println("populate cache " + LocalDateTime.now());
Thread.sleep(2000);
//if (counter.incrementAndGet() >= 2) throw new Exception("error");
return counter.incrementAndGet() * 10;
}
public static void main(String[] args) throws Exception {
useAsyncCache();
}
static void useAsyncCache() throws Exception {
for (int i = 0; i < 10; i++) {
System.out.println(refreshCache.get(0) + " -- " + LocalDateTime.now());
Thread.sleep(1000);
}
}
}
Console output:
populate cache 2017-06-12T15:21:11.026
10 -- 2017-06-12T15:21:13.028
10 -- 2017-06-12T15:21:14.029
10 -- 2017-06-12T15:21:15.029
populate cache 2017-06-12T15:21:16.046
10 -- 2017-06-12T15:21:16.068
10 -- 2017-06-12T15:21:17.069
20 -- 2017-06-12T15:21:18.069
20 -- 2017-06-12T15:21:19.069
20 -- 2017-06-12T15:21:20.069
20 -- 2017-06-12T15:21:21.069
populate cache 2017-06-12T15:21:21.069
20 -- 2017-06-12T15:21:22.070
Be very careful that if we omit the reload method when implementing CacheLoader, the cache will not be load in async way anymore.
注意下,如果不小心忘了重写 reload 方法,缓存将不再被异步刷新。
// public ListenableFuture<Integer> reload(Integer key, Integer oldValue) throws Exception {
// return exe.submit(() -> getIntegerCache());
// }
populate cache 2017-06-12T15:30:58.924 10 -- 2017-06-12T15:31:00.928 10 -- 2017-06-12T15:31:01.928 10 -- 2017-06-12T15:31:02.928 populate cache 2017-06-12T15:31:03.929 20 -- 2017-06-12T15:31:05.960 20 -- 2017-06-12T15:31:06.960 20 -- 2017-06-12T15:31:07.960 populate cache 2017-06-12T15:31:08.961 30 -- 2017-06-12T15:31:10.961 30 -- 2017-06-12T15:31:11.962 30 -- 2017-06-12T15:31:12.962 populate cache 2017-06-12T15:31:13.962 40 -- 2017-06-12T15:31:15.963
Previously cache can be retrieved immediately. After removing the reload method, the get method will be blocked until load method completes.
上图的日志就是在注释掉 reload 方法的情况下得到的。可以明显的看到当缓存过期之后,再次取缓存会被缓慢的 load 方法阻塞掉。
Hmmm, what happens if an error occurs during cache loading? Let's modify the loading part a little bit.
对了,如果 load 方法执行过程中不小心出错会怎么样呢? 我们来稍微修改下加载逻辑。
private static Integer getIntegerCache() throws Exception {
System.out.println("populate cache " + LocalDateTime.now());
Thread.sleep(2000);
if (counter.incrementAndGet() >= 2) throw new Exception("error");
return counter.get() * 10;
}
Accordign to the output, guava gives warning message, but the old value is still returned.
通过输出日志可以看到,Guava会抛出警告日志,返回旧值。
populate cache 2017-06-12T15:37:08.437
10 -- 2017-06-12T15:37:10.440
10 -- 2017-06-12T15:37:11.441
10 -- 2017-06-12T15:37:12.441
populate cache 2017-06-12T15:37:13.441
Jun 12, 2017 3:37:15 PM com.google.common.cache.LocalCache$Segment$1 run
WARNING: Exception thrown during refresh
java.util.concurrent.ExecutionException: java.lang.Exception: error
at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:299)
at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:286)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
at com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly(Uninterruptibles.java:135)
at com.google.common.cache.LocalCache$Segment.getAndRecordStats(LocalCache.java:2346)
at com.google.common.cache.LocalCache$Segment$1.run(LocalCache.java:2329)
at com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:297)
at com.google.common.util.concurrent.ExecutionList.executeListener(ExecutionList.java:156)
at com.google.common.util.concurrent.ExecutionList.add(ExecutionList.java:101)
at com.google.common.util.concurrent.AbstractFuture.addListener(AbstractFuture.java:170)
at com.google.common.cache.LocalCache$Segment.loadAsync(LocalCache.java:2324)
at com.google.common.cache.LocalCache$Segment.refresh(LocalCache.java:2387)
at com.google.common.cache.LocalCache$Segment.scheduleRefresh(LocalCache.java:2365)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2185)
at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
at cachecmp.GuavaAsyncCacheExample.useAsyncCache(GuavaAsyncCacheExample.java:49)
at cachecmp.GuavaAsyncCacheExample.main(GuavaAsyncCacheExample.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.Exception: error
at cachecmp.GuavaAsyncCacheExample.getIntegerCache(GuavaAsyncCacheExample.java:39)
at cachecmp.GuavaAsyncCacheExample.access$000(GuavaAsyncCacheExample.java:17)
at cachecmp.GuavaAsyncCacheExample$1.load(GuavaAsyncCacheExample.java:28)
at cachecmp.GuavaAsyncCacheExample$1.load(GuavaAsyncCacheExample.java:25)
at com.google.common.cache.CacheLoader.reload(CacheLoader.java:97)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
at com.google.common.cache.LocalCache$Segment.loadAsync(LocalCache.java:2323)
... 13 more
10 -- 2017-06-12T15:37:15.475
populate cache 2017-06-12T15:37:16.476
Jun 12, 2017 3:37:18 PM com.google.common.cache.LocalCache$Segment$1 run
10 -- 2017-06-12T15:37:18.478
2. Caffiene
public class CaffeineAsyncCacheExample {
static AtomicInteger counter = new AtomicInteger(0);
static LoadingCache<Integer, Integer> refreshCache = Caffeine.newBuilder()
.refreshAfterWrite(3, TimeUnit.SECONDS)
.recordStats()
.build(new CacheLoader<Integer, Integer>() {
@Override
public Integer load(Integer key) throws Exception {
return getIntegerCache();
}
});
private static Integer getIntegerCache() throws Exception {
System.out.println("populate cache " + LocalDateTime.now());
Thread.sleep(2000);
return counter.incrementAndGet() * 10;
}
public static void main(String[] args) throws Exception {
useAsyncCache();
}
static void useAsyncCache() throws Exception {
for (int i = 0; i < 10; i++) {
System.out.println(refreshCache.get(0) + " -- " + LocalDateTime.now());
Thread.sleep(1000);
}
}
}
Opps, again, the reload method is omitted. But the output looks perfectly fine, what happened?
啊哦,一不小心又忘记写 reload 方法了。 然而仔细一看输出日志,竟毫无阻塞的问题。
populate cache 2017-06-12T15:49:48.043 10 -- 2017-06-12T15:49:50.047 10 -- 2017-06-12T15:49:51.047 10 -- 2017-06-12T15:49:52.047 populate cache 2017-06-12T15:49:53.063 10 -- 2017-06-12T15:49:53.086 10 -- 2017-06-12T15:49:54.087 20 -- 2017-06-12T15:49:55.088 20 -- 2017-06-12T15:49:56.088 20 -- 2017-06-12T15:49:57.089 20 -- 2017-06-12T15:49:58.089 populate cache 2017-06-12T15:49:58.089 20 -- 2017-06-12T15:49:59.090
In order to dig out the truth, let's take a look at the source code of CacheLoader.
为了探索背后的原因,我们来看看CacheLoader的源码
Guava
public abstract class CacheLoader<K, V> {
protected CacheLoader() {
}
public abstract V load(K var1) throws Exception;
@GwtIncompatible("Futures")
public ListenableFuture<V> reload(K key, V oldValue) throws Exception {
Preconditions.checkNotNull(key);
Preconditions.checkNotNull(oldValue);
return Futures.immediateFuture(this.load(key));
}
......
Caffiene
@FunctionalInterface
@ThreadSafe
public interface CacheLoader<K, V> extends AsyncCacheLoader<K, V> {
@CheckForNull
V load(@Nonnull K var1) throws Exception;
@CheckForNull
default V reload(@Nonnull K key, @Nonnull V oldValue) throws Exception {
return this.load(key);
}
@Nonnull
default CompletableFuture<V> asyncReload(@Nonnull K key, @Nonnull V oldValue, @Nonnull Executor executor) {
Objects.requireNonNull(key);
Objects.requireNonNull(executor);
return CompletableFuture.supplyAsync(() -> {
try {
return this.reload(key, oldValue);
} catch (RuntimeException var4) {
throw var4;
} catch (Exception var5) {
throw new CompletionException(var5);
}
}, executor);
}
...
By default, Guava calls reload method while Caffiene calls asyncReload method, that's why we only need to rewrite load method in Caffiene in stead. It saves us some coding effort, but more importantly, it decreases the possibility to make mistakes. For example, some one may forgot to update the reload method after making some changes in load block.
默认情况下,Guava 调用 reload 方法,reload 又会去阻塞调用 load 方法, 而Caffiene 默认调用 asyncReload 方法,并不阻塞,这就是为什么使用 Caffiene 的时候只需要重写 load 方法即可。这不光是节省了一点代码,更重要的,它降低了之后出错的几率。很有可能某位别的程序员日后修改了 load 方法的实现 却忘记了 reload 方法,于是出现错误。
A comparison of local caches (2) 【本地缓存之比较 (2)】的更多相关文章
- A comparison of local caches (1) 【本地缓存之比较 (1)】
1. Spring local cache [Spring 本地缓存] Spring provided cacheable annotation since 3.1. It's very supe ...
- 八、React实战:可交互待办事务表(表单使用、数据的本地缓存local srtorage、生命同期函数(页面加载就会执行函数名固定为componentDidMount()))
一.项目功能概述 示例网址:http://www.todolist.cn/ 功能: 输入待做事项,回车,把任务添加到 [正在进行] [正在进行] 任务,勾选之后,变成已[经完成事项] [已完成事务], ...
- spring boot: 用redis的消息订阅功能更新应用内的caffeine本地缓存(spring boot 2.3.2)
一,为什么要更新caffeine缓存? 1,caffeine缓存的优点和缺点 生产环境中,caffeine缓存是我们在应用中使用的本地缓存, 它的优势在于存在于应用内,访问速度最快,通常都不到1ms就 ...
- spring boot:使用spring cache+caffeine做进程内缓存(本地缓存)(spring boot 2.3.1)
一,为什么要使用caffeine做本地缓存? 1,spring boot默认集成的进程内缓存在1.x时代是guava cache 在2.x时代更新成了caffeine, 功能上差别不大,但后者在性能上 ...
- Java8简单的本地缓存实现
原文出处:lukaseder Java8简单的本地缓存实现 这里我将会给大家演示用ConcurrentHashMap类和lambda表达式实现一个本地缓存.因为Map有一个新的方法,在 ...
- iOS五种本地缓存数据方式
iOS五种本地缓存数据方式 iOS本地缓存数据方式有五种:前言 1.直接写文件方式:可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...
- ImageLoader(多线程网络图片加载)+本地缓存 for windowsphone 7
搞了好长一阵子wp,做点好事. C/S手机app中应用最多的是 获取网络图片,缓存到本地,展示图片 本次主要对其中的delay:LowProfileImageLoader进行修改,在获取图片的时候, ...
- lua模块demo(redis,http,mysql,cjson,本地缓存)
1. lua模块demo(redis,http,mysql,cjson,本地缓存) 1.1. 配置 在nginx.conf中设置lua_shared_dict my_cache 128m; 开启ngi ...
- ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪
ASP.NET MVC深入浅出(被替换) 一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...
随机推荐
- JS模式--通用对象池的实现
var objectPoolFactory = function (createObjFn) { var objectPool = []; return { create: function () { ...
- javascript执行原理
执行环境 当执行流执行到函数时会创建一个执行环境,这个执行环境包含了函数内部 语句可以访问的所有变量和函数,当代码执行完时,销毁执行环境.所以一般情 况下,局部变量在函数执行完时会被销毁. 作用域.调 ...
- java web 简单的登录注册
--sql文件 create database studentgouse studentgocreate table stuinfo(--stuid int primary key identity( ...
- C语言中的指针
请先看C++中的指针概述,这里只是扩充 数组指针 其实这里主要说的就是c++中的指针运算 /* 数组元素指针: 一个变量有地址,一个数组包含若干个元素,每个数组元素都有相应的地址, 指针变量可以指向数 ...
- Linux 定时任务详解
原文地址:http://edu.codepub.com/2011/0104/28518.php crond分为系统级定时和用户级定时,系统级定时主要编辑/etc/crontab,用户级定时主要利用 ...
- 使用openXML 不用插件导出excel
注释很详细,不做解释了,有疑问可以提问 using System.IO; using System.Text; namespace iLIS.Common { /// <summary> ...
- silverlight WPF 水纹文本框
silverlight3取消了watertextbox控件,只有自己实现了个,实现了和textbox一样的无差异使用,只需要设置defaulttext就可以了 using System; using ...
- PHP实现类似于MySQ L的group by 效果
简单版本:只用于获取组数据 其实就是将键值变成键,遍历的时候键相同的会覆盖 $res[$v['country']] [] 可以 = 1,无关紧要
- 介绍几个python的音频处理库
一.eyeD3 直接在google上搜索python mp3 process ,推荐比较多的就是这个第三方库了.先来看看官方介绍吧. About eyeD3 is a Python tool for ...
- 零售业山重水复,全景行柳暗花明——VR全景智慧城市
对今天的中国来说,寻找经济转型的突破口,寻找经济权力的新霸主,零售业应该当仁不让. 零售业正在经历一场脱胎换骨的改造.一方面零售额达到前所未有的水平,另一方面,传统零售商也面临诸多挑战,其中之一,便是 ...