Guava Cache LoadingCache 之 refreshAfterWrites和expireAfterWrite区别 使用说明

三种基于时间清理或刷新缓存数据的方式:

expireAfterAccess: 当缓存项在指定的时间段内没有被读或写就会被回收。

expireAfterWrite:当缓存项在指定的时间段内没有更新就会被回收(移除key),需要等待获取新值才会返回。

refreshAfterWrite:当缓存项上一次更新操作之后的多久会被刷新。第一个请求进来,执行load把数据加载到内存中(同步过程),指定的过期时间内比如10秒,都是从cache里读取数据。过了10秒后,没有请求进来,不会移除key。再有请求过来,才则执行reload,在后台异步刷新的过程中,如果当前是刷新状态,访问到的是旧值。刷新过程中只有一个线程在执行刷新操作,不会出现多个线程同时刷新同一个key的缓存。在吞吐量很低的情况下,如很长一段时间内没有请求,再次请求有可能会得到一个旧值(这个旧值可能来自于很长时间之前),这将会引发问题。(可以使用expireAfterWrite和refreshAfterWrite搭配使用解决这个问题


	com.google.guava
	guava
	23.0

refreshAfterWrites  例子

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;

/**
 *  对于一些数据查询比较耗时,缓解服务器的压力,高并发的场景下,可以使用refreshAfterWrites这种异步刷新缓存的方法。
 *	问题:
 *	1、会不会出现取到的值是过期的旧值?
 *	2、随着请求的快速上升,连接数是否足够?
 *
 * LoadingCache 之 refreshAfterWrites 刷新机制
 * 把数据加入到缓存中,并异步刷新缓存里的数据
 * 
 * @author cenjianteng
 */
public class LoadingCacheDemo {

	public LoadingCache> cache;

	private static ExecutorService executorService = Executors.newFixedThreadPool(3);

	/**
	 * 加入cache的数据
	 */
	private Map setData() {
		Map result = new HashMap<>();

		result.put("result1", "小明");
		result.put("result2", "小黑");
		result.put("result3", "小白");
		result.put("result4", "小黄");

		return result;
	}

	/**
	 * 构建缓存对象
	 */
	private void buildCache() {

		cache = CacheBuilder.newBuilder().refreshAfterWrite(8, TimeUnit.SECONDS)
				.build(new CacheLoader>() {
					@Override
					public Map load(String s) throws Exception {
						System.out.println("load ...");
						return setData();
					}

					@Override
					public ListenableFuture> reload(String key, Map oldValue)
							throws Exception {
						System.out.println("reload ...");
						ListenableFutureTask> task = ListenableFutureTask
								.create(new Callable>() {
									@Override
									public Map call() throws Exception {
										return setData();
									}
								});
						executorService.execute(task);
						return task;
					}
				});

	}

	/**
	 * 测试
	 */
	public static void main(String[] args) {

		LoadingCacheDemo demo = new LoadingCacheDemo();
		demo.buildCache();

		Runnable runnable1 = () -> {
			for (int i = 0; i < 100; i++) {
				try {
					System.out.println("Runnable1 Before Get Cache");
					System.out.println("Runnable1 " + demo.cache.get("test"));
					System.out.println("Runnable1 After Get Cache");
					Thread.currentThread().sleep(1000);
				} catch (ExecutionException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		Runnable runnable2 = () -> {
			for (int i = 0; i < 100; i++) {
				try {
					System.out.println("Runnable2 Before Get Cache");
					System.out.println("Runnable2 " + demo.cache.get("test"));
					System.out.println("Runnable2 After Get Cache");
					Thread.currentThread().sleep(1000);
				} catch (ExecutionException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		Thread thread1 = new Thread(runnable1);
		Thread thread2 = new Thread(runnable2);
		thread1.start();
		thread2.start();

	}

}

 expireAfterWrite 例子

package com.cjt.demo;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CacheDemo2 {

    private static Cache cache = CacheBuilder.newBuilder().maximumSize(1).
            expireAfterWrite(10, TimeUnit.SECONDS).build();

    public static void main(String[] args) {

        try {

            Runnable runnable1 = () -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        String str = cache.get("123", new Callable() {
                            public String call() throws Exception {
                                cache.put("123","123");
                                return "111111111111111";
                            }
                        });
                        System.out.println(str);

                        Thread.currentThread().sleep(1000);
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            Runnable runnable2 = () -> {
                for (int i = 0; i < 10; i++) {
                    try {
                        System.out.println(cache.get("456", new Callable() {
                            public String call() throws Exception {
                                cache.put("456","456");
                                return "222222222222222";
                            }
                        }));
                        Thread.currentThread().sleep(1000);
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            Thread thread1 = new Thread(runnable1);
            Thread thread2 = new Thread(runnable2);
            thread1.start();
            thread2.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

expireAfterWrite和refreshAfterWrite搭配使用

.refreshAfterWrite(20, TimeUnit.MINUTES) 
.expireAfterWrite(30, TimeUnit.MINUTES) 
第一次请求执行load,直到20分钟内,都是读取内存。20分钟后取决于有没有请求读取cache 使其执行reload刷新内存的值,没有请求就延后30分钟看内存有没有被刷新(expireAfterWrite),没有刷新则移除key,下次读取则重新加载,保证获取的是近期的新值。如果一直有请求进来,就不会移除key,执行refreshAfterWrite的异步刷新

部分代码如下:

 CacheBuilder.newBuilder()
                .refreshAfterWrite(20, TimeUnit.MINUTES)
                .expireAfterWrite(30, TimeUnit.MINUTES)
                .maximumSize(1)
                .build(new CacheLoader>>() {
                    @Override
                    public List> load(String s) throws Exception {
                        return queryData();
                    }

                    @Override
                    public ListenableFuture>> reload(String key, List> oldValue)
                            throws Exception {
                        ListenableFutureTask>> task = ListenableFutureTask
                                .create(() -> queryData());
                        executorService.execute(task);
                        return task;
                    }
                });

 

你可能感兴趣的:(java,LoadingCache,guava,cache)