问题描述
我使用caffeine库遇到了异步缓存。在https://www.programcreek.com/java-api-examples/?api=com.github.benmanes.caffeine.cache.AsyncCacheLoader之后,有以下代码段:
first_name | middle_name | last_name
------------------------------------
James | Leroy | Bond
- 很明显,以上代码将在我返回
import com.github.benmanes.caffeine.cache.AsyncCacheLoader; import com.github.benmanes.caffeine.cache.AsyncLoadingCache; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.caffeine; import com.github.benmanes.caffeine.cache.Weigher; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; public class CacheManager { private static AsyncLoadingCache<String,Object> loader; public static void initLoadingCache(<datatype> obj) { if (loader == null) { loader = caffeine.newBuilder() .concurrencyLevel(10) .expireAfterWrite(60,TimeUnit.MINUTES) // Cache will expire after 60 minutes .buildAsync(new AsyncCacheLoader<String,Object>() { // Build the CacheLoader @Override public CompletableFuture<Object> asyncLoad(String key,Executor executor) throws Exception{ Object temp = obj.getTemp(key); // my function which does processing if (temp == null) LOGGER.error("Not found"); return temp; } }); } } } public Object getTemp(String key) { Query query = somequery(); List<Object> val = query.getResultList(); return val.get(0); }
的{{1}}时出错,但是该函数期望temp
。我该如何实现?我猜测Object
也需要返回CompletableFuture<Object>
(https://www.programcreek.com/java-api-examples/?code=Netflix%2Ftitus-control-plane%2Ftitus-control-plane-master%2Ftitus-supplementary-component%2Ftasks-publisher%2Fsrc%2Fmain%2Fjava%2Fcom%2Fnetflix%2Ftitus%2Fsupplementary%2Ftaskspublisher%2FTitusClientImpl.java),但是由于它具有数据库查询,因此我不确定如何实现。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)