如何解决使用咖啡因的异步缓存
我使用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 举报,一经查实,本站将立刻删除。