gauva cache

guava 的cache比较好用。
 
 用户使用的对象是LoadingCache, 通过CacheBuilder来创建,通过
CacheLoader来根据key加载数据。而且可以定时刷新缓存(有访问才异步刷新缓存,可以让缓存自动过期。)
 
 
 
 
 
package testjava;

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

import java.util.concurrent.*;

/**
 * Create with test01
 * Auther: hp.wang on 2017/9/22
 * DateTime: 2017/9/22 13:20
 */
public class testCache {
    static ExecutorService executor = Executors.newFixedThreadPool(10);
static Integer i1=0;
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // Some keys don't need refreshing, and we want refreshes to be done asynchronously.
        LoadingCache<String, String> cache = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterAccess(7, TimeUnit.SECONDS)
                .refreshAfterWrite(2, TimeUnit.SECONDS)
                .build(
                        new CacheLoader<String, String>() {
                            public String load(String key) { // no checked exception
                                return "load";
                            }

                            public ListenableFuture<String> reload(final String key, String prevGraph) {

                                // asynchronous!
                                ListenableFutureTask<String> task = ListenableFutureTask.create(new Callable<String>() {
                                    final int i = 0;

                                    public String call() throws Exception {
                                        Thread.sleep(152);
                                        i1 = i1 + 1;
                                        System.out.println("reloading." + i1);
                                        return "reload value:" + i1;
                                    }
                                });
                                executor.execute(task);
                                return task;

                            }
                        });

        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            System.out.println(cache.get("aa"));
        }
        System.out.println("=== wait for expired.  ================");
        Thread.sleep(10000);

        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            System.out.println(cache.get("aa"));
        }
        System.out.println("===================");
    }
}

 output:

===============================

load
load
load
reloading.1
reload value:1
reload value:1
reload value:1
reloading.2
reload value:2
reload value:2
reload value:2
reloading.3
reload value:3
=== wait for expired. ================
load
load
load
load
reloading.4
reload value:4
reload value:4
reload value:4
reloading.5
reload value:5
reload value:5
reload value:5
===================
reloading.6

posted on 2017-09-22 13:47  Henry_Wang  阅读(207)  评论(0编辑  收藏  举报

导航