如何从Kotlin的协程仪获得返回值?

我正在使用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是一种暂停的娱乐,因此我只能从协程或其他暂停的娱乐中调用它:(

njepc 回答:如何从Kotlin的协程仪获得返回值?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2730524.html

大家都在问