gpt4 book ai didi

java - 咖啡因:当AsyncLoader无法刷新时,请使用过时的值

转载 作者:行者123 更新时间:2023-12-02 13:17:42 26 4
gpt4 key购买 nike

我想配置Caffeine缓存以在加载程序无法刷新缓存时返回陈旧的结果。以下Kotlin代码演示了这种情况:

    @Test
fun `completeble future`() = runBlocking {
val cache = Caffeine.newBuilder()
.refreshAfterWrite(Duration.ofSeconds(1))
.expireAfterWrite(Duration.ofSeconds(1))
.buildAsync<String, String> { key: String, executor ->
GlobalScope.future(executor.asCoroutineDispatcher()) {
throw Exception("== Error ==")
}
}

cache.put("id", CompletableFuture.completedFuture("value"))

delay(2000)

assertEquals("value", cache.get("id").await())
}

我希望此测试通过,但会收到以下错误:
WARNING: Exception thrown during asynchronous load
java.lang.Exception: == Error ==
at fsra.manager.TranslationManagerImplTest$completeble future$1$cache$1$1.invokeSuspend(TranslationManagerImplTest.kt:93)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)




java.lang.Exception: == Error ==

at fsra.manager.TranslationManagerImplTest$completeble future$1$cache$1$1.invokeSuspend(TranslationManagerImplTest.kt:93)
at |b|b|b(Coroutine boundary.|b(|b)
at fsra.manager.TranslationManagerImplTest$completeble future$1.invokeSuspend(TranslationManagerImplTest.kt:101)
Caused by: java.lang.Exception: == Error ==
at fsra.manager.TranslationManagerImplTest$completeble future$1$cache$1$1.invokeSuspend(TranslationManagerImplTest.kt:93)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)

我用Kotlin编写了代码,但我认为该问题与Kotlin协程无关。我想将Caffeine配置为在刷新时不抛出,而是在缓存中返回先前的结果。

最佳答案

高速缓存设置为在1秒后过期条目,测试等待2秒。然后,由于该条目不可用,因此下一个调用将强制重新加载它,并且引发异常。

大于或等于到期时间时,刷新不起作用。小于时,该条目是陈旧但可用的,因此将其返回并异步重新加载。这是为了使热门项目(例如配置)可以保留在缓存中,而不会导致定期重新加载。不受欢迎的项目是在到期间隔内未访问的项目,可以将其逐出。如果刷新无法成功,则到期将开始,条目将被删除,因为到期设置了认为可用的最大时间。

较大的到期值(例如5秒)将通过测试。如果您的用例是定期盲目地重新加载所有缓存内容,则可以使用常规的MapScheduledExecutorService刷新它。

关于java - 咖啡因:当AsyncLoader无法刷新时,请使用过时的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com