本文共 4136 字,大约阅读时间需要 13 分钟。
上一篇文章""提到:如果缓存过期,恰好有多个线程读取同一个key的值,那么guava只允许一个线程去加载数据,其余线程阻塞。这虽然可以防止大量请求穿透缓存,但是效率低下。使用refreshAfterWrite可以做到:只阻塞加载数据的线程,其余线程返回旧数据。
public class GuavaCache4TestRefresh { private static CountDownLatch latch = new CountDownLatch(1); // 1s后刷新缓存 private static LoadingCachecache = CacheBuilder.newBuilder().refreshAfterWrite(1, TimeUnit.SECONDS) .build(new CacheLoader () { @Override public String load(String key) throws Exception { return callable.call(); } }); // 模拟一个需要耗时2s的数据库查询任务 private static Callable callable = new Callable () { @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...beginThread-2...beginThread-1...beginThread-3...beginThread-4...beginThread-5...beginThread-6...beginThread-7...beginbegin to mock query db...Thread-1...value...atyThread-0...value...atyThread-1...finish,cost time=0Thread-3...value...atyThread-5...value...atyThread-7...value...atyThread-5...finish,cost time=0Thread-3...finish,cost time=0Thread-4...value...atyThread-0...finish,cost time=0Thread-6...value...atyThread-4...finish,cost time=0Thread-7...finish,cost time=0Thread-6...finish,cost time=0success to mock query db...Thread-2...value...bbae863c-bd28-4744-bd5e-227300d1662aThread-2...finish,cost time=2
通过输出结果可以看出:当缓存数据过期的时候,只有Thread-2真正去加载数据的线程会阻塞一段时间,其余线程立马返回过期的值,显然这种处理方式更符合实际的使用场景。
有一点需要注意:我们手动向缓存中添加了一条数据,并让其过期。如果没有这行代码,程序执行结果如下。
Thread-0...beginThread-2...beginThread-1...beginThread-3...beginThread-4...beginThread-5...beginThread-6...beginThread-7...beginbegin to mock query db...success to mock query db...Thread-0...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-5...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-1...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-5...finish,cost time=2Thread-7...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-2...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-0...finish,cost time=2Thread-4...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-3...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-4...finish,cost time=2Thread-2...finish,cost time=2Thread-7...finish,cost time=2Thread-1...finish,cost time=2Thread-3...finish,cost time=2Thread-6...value...cc22caf5-23bb-4365-b143-6d5d64447cc9Thread-6...finish,cost time=2
由于缓存没有数据,导致一个线程去加载数据的时候,别的线程都阻塞了(因为没有旧值可以返回)。所以一般系统启动的时候,我们需要将数据预先加载到缓存,不然就会出现这种情况。
还有一个问题不爽:真正加载数据的那个线程一定会阻塞,我们希望这个加载过程是异步的。这样就可以让所有线程立马返回旧值,在后台刷新缓存数据。refreshAfterWrite默认的刷新是同步的,会在调用者的线程中执行。我们可以改造成异步的,实现CacheLoader.reload(),对上面代码进行如下修改:
// guava线程池,用来产生ListenableFuture private static ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); // 1s后刷新缓存 private static LoadingCachecache = CacheBuilder.newBuilder().refreshAfterWrite(1, TimeUnit.SECONDS) .build(new CacheLoader () { @Override public String load(String key) throws Exception { return callable.call(); } @Override public ListenableFuture reload(String key, String oldValue) throws Exception { System.out.println("......后台线程池异步刷新:" + key); return service.submit(callable); } });
转载地址:http://gfdoi.baihongyu.com/