guava测试例子,取值和定时重新装载

package com.taobao.saleengine.core.processor.onlinelearning.service.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.concurrent.ExecutionException;
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.Futures;
import com.google.common.util.concurrent.ListenableFuture;

public class Test {

    public static void main(String[] args) {
        final Test xx = new Test();
        Thread t = new Thread("aaa") {
            public void run() {
                try {
                    System.out.println("0秒");
                    System.out.println(xx.get("1"));
                    System.out.println(xx.get("2"));
                    System.out.println(xx.get("3"));
                    System.out.println(xx.get("4"));
                    System.out.println(xx.get("5"));
                    Thread.sleep(20 * 1000);
                    System.out.println("20秒");
                    System.out.println(xx.get("1"));
                    System.out.println(xx.get("2"));
                    System.out.println(xx.get("3"));
                    System.out.println(xx.get("4"));
                    System.out.println(xx.get("5"));
                    Thread.sleep(20 * 1000);
                    System.out.println("40秒");
                    System.out.println(xx.get("1"));
                    System.out.println(xx.get("2"));
                    System.out.println(xx.get("3"));
                    System.out.println(xx.get("4"));
                    System.out.println(xx.get("5"));
                    Thread.sleep(20 * 1000);
                    System.out.println("60秒");
                    System.out.println(xx.get("1"));
                    System.out.println(xx.get("2"));
                    System.out.println(xx.get("3"));
                    System.out.println(xx.get("4"));
                    System.out.println(xx.get("5"));
                    Thread.sleep(20 * 1000);
                    System.out.println("80秒");
                    System.out.println(xx.get("1"));
                    System.out.println(xx.get("2"));
                    System.out.println(xx.get("3"));
                    System.out.println(xx.get("4"));
                    System.out.println(xx.get("5"));
                    Thread.sleep(20 * 1000);
                    System.out.println("100秒");
                    System.out.println(xx.get("1"));
                    System.out.println(xx.get("2"));
                    System.out.println(xx.get("3"));
                    System.out.println(xx.get("4"));
                    System.out.println(xx.get("5"));
                } catch (Exception e) {
                    System.out.println("error!");
                }
            }
        };
        t.start();
    }
    
    public String get(String key) throws ExecutionException {
        return localModelCache.get(key);
    }

    // 缓存模型
    private final LoadingCache<String, String> localModelCache = CacheBuilder.newBuilder().refreshAfterWrite(1, TimeUnit.MINUTES).build(new CacheLoader<String, String>() {

          @Override
          public String load(String key) throws Exception {
              return loadModelFeature(key);
          }

        @Override
        public ListenableFuture<String> reload(String key, String oldValue) throws Exception {
            System.out.println("reload");
            return reloadListenableFuture(key, oldValue);
        }
      });
    
    public ListenableFuture<String> reloadListenableFuture(String key, String oldValue) throws Exception {
        checkNotNull(key);
        checkNotNull(oldValue);
        return Futures.immediateFuture(oldValue + "reloadable");
      }
    
    private String loadModelFeature(String key) {
        System.out.println("===================" + "从本地装载" + key + "===================");
        return key + "1";
    }
    
}

 

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