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

  • 相关阅读:
    音频、视频等文件格式(.ts、.meta)及其认识
    元素之和最接近 0 的区间(部分和)
    元素之和最接近 0 的区间(部分和)
    说话的智慧
    说话的智慧
    工业相机基本参数以及选型参考(二)
    常用编程软件站点、镜像站、科技类 PDF
    常用编程软件站点、镜像站、科技类 PDF
    经典书单 —— 语言/算法/机器学习/深度学习/AI/CV/PGM
    hdu1024 Max Sum Plus Plus
  • 原文地址:https://www.cnblogs.com/netact/p/7574486.html
Copyright © 2011-2022 走看看