我正在使用Caffeine和Kotlin构建缓存。我有这样的东西:
private val esCache: Cache<EsRequestParam, EsResponse> = Caffeine.newBuilder()
.expireAfterWrite(30, TimeUnit.SECONDS)
.refreshAfterWrite(50, TimeUnit.SECONDS)
.build<EsRequestParam, EsResponse> {
runBlocking {
sendRequest(it)
}
}
private suspend fun sendRequest(requestParams: EsRequestParam): EsResponse? {
val result = client
.sendJsonObjectAwait(
JsonObject(
buildPostBody(requestParams)
))
.bodyAsJson(EsResponse::class.java)
esCache.put(requestParams, result)
return result
}
由于我也使用Vertx,所以我知道它具有一个事件循环,该循环可能被阻止并制动我的应用程序。 runBlocking
给我带来了这个问题。因此,我需要将此runBlocking
交换给其他一些Vertx资源或GlobalScope.launch(vertx.dispacther())
。然后我可以避免阻塞事件。
问题是:我不知道我可以使用哪个Vertx资源,也不能使用GlobalScope.launch
,因为它返回一个Job,而不是sendRequest
函数的返回值。
有任何想法吗?
附注:由于sendRequest
是一种暂停的娱乐,因此我只能从协程或其他暂停的娱乐中调用它:(