涉及 Scalaz 的 Play 框架中基于键的缓存
Key-Based Caching in Play Framework with Scalaz Involved
我正在尝试为我的 Play Framework 应用程序做一些基于键的缓存。这是我端点处处理程序的代码:
optionT(findById(id)).map { thing =>
val cacheKey = thingCacheKey(thing)
cached(cacheKey) {
Action.async { implicit request =>
val result = //Produce a result—probably Ok(“content”)
} yield result
}
}
}.run.map {
case Some(actionF) => actionF
case None => Action.async(Future.successful(NotFound()))
}
如您所见,我正在使用 Scalaz 的 optionT
monad 转换器。
不足为奇,我得到了这个错误:
不能使用方法 returning scala.concurrent.Future[play.api.mvc.EssentialAction] 作为请求的处理程序
你能告诉我如何用这种方法 return Result
而不是 EssentialAction
吗?
播放缓存 api 没有任何 monadic 作曲家,也没有 Action
所以真的需要 运行 直到 Iteratee
有 flatten
方法的动作可以解开这个 Future
。这是成功编译方法的例子
Action.async { implicit request => Iteratee.flatten {
optionT(findById(id)).map { thing =>
val cacheKey = thingCacheKey(thing)
val action = cached(cacheKey) {
Action.async{
Future(Ok) // replace with your processing of "request"
}
}
action.apply(request)
}.getOrElse(Iteratee.ignore[Array[Byte]].map(_ => NotFound))
}.run
}
这里我们通过getOrElse
而不是run.map{ }
展开OptionT
变压器,然后运行宁Iteratee
到Future[Result]
我正在尝试为我的 Play Framework 应用程序做一些基于键的缓存。这是我端点处处理程序的代码:
optionT(findById(id)).map { thing =>
val cacheKey = thingCacheKey(thing)
cached(cacheKey) {
Action.async { implicit request =>
val result = //Produce a result—probably Ok(“content”)
} yield result
}
}
}.run.map {
case Some(actionF) => actionF
case None => Action.async(Future.successful(NotFound()))
}
如您所见,我正在使用 Scalaz 的 optionT
monad 转换器。
不足为奇,我得到了这个错误:
不能使用方法 returning scala.concurrent.Future[play.api.mvc.EssentialAction] 作为请求的处理程序
你能告诉我如何用这种方法 return Result
而不是 EssentialAction
吗?
播放缓存 api 没有任何 monadic 作曲家,也没有 Action
所以真的需要 运行 直到 Iteratee
有 flatten
方法的动作可以解开这个 Future
。这是成功编译方法的例子
Action.async { implicit request => Iteratee.flatten {
optionT(findById(id)).map { thing =>
val cacheKey = thingCacheKey(thing)
val action = cached(cacheKey) {
Action.async{
Future(Ok) // replace with your processing of "request"
}
}
action.apply(request)
}.getOrElse(Iteratee.ignore[Array[Byte]].map(_ => NotFound))
}.run
}
这里我们通过getOrElse
而不是run.map{ }
展开OptionT
变压器,然后运行宁Iteratee
到Future[Result]