0

我正在尝试将 UserAwareAction 与缓存结合起来。我能够获得 CacheBeforeSilhouette 但不能反过来?谁能给我一个关于如何做到这一点的提示?

@Singleton
class MessageController @Inject() (
  implicit val env: Environment[DefaultEnv],
  silhouette: Silhouette[DefaultEnv],
  cache: CacheApi,
  cached: play.api.cache.Cached)
    extends Controller with I18nSupport {

...

  def testOnlySilhouette = silhouette.UserAwareAction { request =>
    Ok("hi")
  }
  def testOnlyCache = cached("homePage") {
    Action {
      Ok("Hello world")
    }
  }
  def testCacheOfSilhouette = cached("homePage") {
    silhouette.UserAwareAction { request =>
      Ok("hi")
    }
  }
  def testSilhouetteOfCache =
    silhouette.UserAwareAction { request =>
      cached("homePage") {
        val res:Result = Ok("hi")
        res //type mismatch; found : play.api.mvc.Result required: play.api.mvc.EssentialAction
      }
    }
4

1 回答 1

0

Christian Kaps @akkie 在 gitter 上回复:

问题首先是 play.api.cache.Cached.apply 函数返回一个需要 EssentialAction 作为参数的 CachedBuilder。所以你必须总是写:

Cached("some-key") {
  Action {
    Ok("")
  }
}

第二个问题是请求类型不兼容。Play 类需要一个 Request[B],但 Silhouette 产生一个 SecuredRequest[E, B]。通过一些手工操作,您可以让它工作:

def testSilhouetteOfCache = 
  silhouette.SecuredAction.async { securedRequest =>
    cached("some-key") {
      Action {
        Ok("")
      }
    }.apply(securedRequest.request).run()
  }

你需要一个隐式的 val 物化器:Materializer injection

于 2017-03-16T00:14:53.317 回答