Guava refreshAfterWrite只阻塞回源线程,其他线程返回旧值

上一篇文章"Guava Cache特性:对于同一个key,只让一个请求回源load数据,其他线程阻塞等待结果"提到:如果缓存过期,恰好有多个线程读取同一个key的值,那么guava只允许一个线程去加载数据,其余线程阻塞。这虽然可以防止大量请求穿透缓存,但是效率低下。使用refreshAfterWrite可以做到:只阻塞加载数据的线程,其余线程返回旧数据。

public class GuavaCache4TestRefresh {

	private static CountDownLatch latch = new CountDownLatch(1);

	// 1s后刷新缓存
	private static LoadingCache<String, String> cache = CacheBuilder.newBuilder().refreshAfterWrite(1, TimeUnit.SECONDS)
			.build(new CacheLoader<String, String>() {
				@Override
				public String load(String key) throws Exception {
					return callable.call();
				}
			});
	
	// 模拟一个需要耗时2s的数据库查询任务
	private static Callable<String> callable = new Callable<String>() {
		@Override
		public String call() throws Exception {
			System.out.println("begin to mock query db...");
			Thread.sleep(2000);
			System.out.println("success to mock query db...");
			return UUID.randomUUID().toString();
		}
	};

	public static void main(String[] args) throws Exception {
		// 手动添加一条缓存数据,睡眠1.5s让其过期
		cache.put("name", "aty");
		Thread.sleep(1500);
		for (int i = 0; i < 8; i++) {
			startThread(i);
		}
		// 让线程运行
		latch.countDown();

	}

	private static void startThread(int id) {

		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					System.out.println(Thread.currentThread().getName() + "...begin");
					latch.await();

					Stopwatch watch = Stopwatch.createStarted();
					System.out.println(Thread.currentThread().getName() + "...value..." + cache.get("name"));
					watch.stop();
					System.out.println(Thread.currentThread().getName() + "...finish,cost time="
							+ watch.elapsed(TimeUnit.SECONDS));

				} catch (Exception e) {

					e.printStackTrace();

				}
			}

		});

		t.setName("Thread-" + id);
		t.start();
	}

}
Thread-0...begin
Thread-2...begin
Thread-1...begin
Thread-3...begin
Thread-4...begin
Thread-5...begin
Thread-6...begin
Thread-7...begin
begin to mock query db...
Thread-1...value...aty
Thread-0...value...aty
Thread-1...finish,cost time=0
Thread-3...value...aty
Thread-5...value...aty
Thread-7...value...aty
Thread-5...finish,cost time=0
Thread-3...finish,cost time=0
Thread-4...value...aty
Thread-0...finish,cost time=0
Thread-6...value...aty
Thread-4...finish,cost time=0
Thread-7...finish,cost time=0
Thread-6...finish,cost time=0
success to mock query db...
Thread-2...value...bbae863c-bd28-4744-bd5e-227300d1662a
Thread-2...finish,cost time=2

通过输出结果可以看出:当缓存数据过期的时候,只有Thread-2真正去加载数据的线程会阻塞一段时间,其余线程立马返回过期的值,显然这种处理方式更符合实际的使用场景。

有一点需要注意:我们手动向缓存中添加了一条数据,并让其过期。如果没有这行代码,程序执行结果如下。

Thread-0...begin
Thread-2...begin
Thread-1...begin
Thread-3...begin
Thread-4...begin
Thread-5...begin
Thread-6...begin
Thread-7...begin
begin to mock query db...
success to mock query db...
Thread-0...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-5...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-1...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-5...finish,cost time=2
Thread-7...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-2...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-0...finish,cost time=2
Thread-4...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-3...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-4...finish,cost time=2
Thread-2...finish,cost time=2
Thread-7...finish,cost time=2
Thread-1...finish,cost time=2
Thread-3...finish,cost time=2
Thread-6...value...cc22caf5-23bb-4365-b143-6d5d64447cc9
Thread-6...finish,cost time=2

由于缓存没有数据,导致一个线程去加载数据的时候,别的线程都阻塞了(因为没有旧值可以返回)。所以一般系统启动的时候,我们需要将数据预先加载到缓存,不然就会出现这种情况。

还有一个问题不爽:真正加载数据的那个线程一定会阻塞,我们希望这个加载过程是异步的。这样就可以让所有线程立马返回旧值,在后台刷新缓存数据。refreshAfterWrite默认的刷新是同步的,会在调用者的线程中执行。我们可以改造成异步的,实现CacheLoader.reload(),对上面代码进行如下修改:

// guava线程池,用来产生ListenableFuture
	private static ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));

	// 1s后刷新缓存
	private static LoadingCache<String, String> cache = CacheBuilder.newBuilder().refreshAfterWrite(1, TimeUnit.SECONDS)
			.build(new CacheLoader<String, String>() {
				@Override
				public String load(String key) throws Exception {
					return callable.call();
				}
				
				@Override
				public ListenableFuture<String> reload(String key, String oldValue) throws Exception {
					System.out.println("......后台线程池异步刷新:" + key);
					return service.submit(callable);

				}
			});
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值