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";
}
}