zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    71)PHP,使用cookie的语法问题
    70)PHP,cookie的安全传输和HTTPonly
    69)PHP,cookie的有效域
    68)PHP,cookie的详细属性和有效期
    C#中的internal关键字
    C# 中如何将一个类文件(XX.CS)封装成.dll文件
    c# 委托和事件(总结篇)
    c#事件实例三
    c#事件实例二
    c#事件实例一
  • 原文地址:https://www.cnblogs.com/netact/p/7574486.html
Copyright © 2011-2022 走看看