Mej*_*jmo 5 kotlin spring-boot caffeine-cache
我正在尝试将咖啡因缓存集成到 kotlin+spring boot 应用程序中,但是,我遇到了在非协程主体中调用暂停函数的问题。我明白了,但我正在寻找一个应该更标准的解决方案。我在网上只能找到一个导致 SO 的解决方案,但我并没有真正找到解决此问题的稳定方法。
inMemoryCache.get(id) { id ->
some call to external service <--- "Suspension function can be called only within coroutine body"
}
Run Code Online (Sandbox Code Playgroud)
您不能在缓存加载函数中使用可挂起函数,因为这些函数不是协程。
您有多种选择。
Cache或LoadingCache您可以使用runBlocking.inMemoryCache.get(id) { id ->
runBlocking {
some call to external service
}
}
Run Code Online (Sandbox Code Playgroud)
AsyncCacheFuture AsyncLoadingCache。请注意,您必须创建一个CoroutineScope才能调用 async.inMemoryCache.get(id) { id, _ ->
scope.async { compute(k) }.asCompletableFuture().await()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3313 次 |
| 最近记录: |