private LoadingCache<String, String> cCodeCache;
private static final String qurl = "http://";
/**
* http 请求 IO型计算 采用线程池
*/
private static ExecutorService executor = new ThreadPoolExecutor(2, 10, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<Runnable>(500),
new ThreadFactory() {
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setDaemon(true);
return thread;
}
}, new ThreadPoolExecutor.DiscardOldestPolicy());
@PostConstruct
private void init() {
cCodeCache = CacheBuilder.newBuilder().maximumSize(500).refreshAfterWrite(2, TimeUnit.HOURS).build(
new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
String cCode = getExpressCompanyCode(key);
return cCode == null ? "" : cCode;
}
@Override
public ListenableFuture<String> reload(final String company, String oldValue) throws Exception {
ListenableFutureTask<String> task = ListenableFutureTask.create(new Callable<String>() {
@Override
public String call() throws Exception {
String cCode = getExpressCCode(company);
return cCode == null ? "" : cCode;
}
});
executor.execute(task);
return task;
}
}
);
}
知识库--本地缓存设计(128)
最新推荐文章于 2024-04-08 15:01:43 发布