【发布时间】:2014-03-30 00:26:31
【问题描述】:
我正在尝试实现一个将请求缓存到外部 API 的系统。如果响应在缓存中,则不应向外部站点发出请求。
我有两种方法:
// Check to see if the response is in the database
def checkCache(searchParameters: JsValue): Future[Option[JsValue]]
// Call the external API and get the JSON response
def getResponse(path: String): Future[JsValue]
然后我尝试执行以下操作:
val json: Future[JsValue] = for {
databaseJson <- checkCache(searchParameters)
externalJson <- getResponse(path)
} yield databaseJson match {
case None => externalJson
case Some(x) => x
}
这可行,但始终向外部 API 发出请求,即使返回缓存结果也是如此。这显然不是我想要的,因为它很慢。
我该如何解决这个问题?
【问题讨论】:
标签: scala playframework playframework-2.0 future scala-2.10