How do we call “Caching in Template” for Play 2.1?

大兔子大兔子 提交于 2019-12-24 10:47:17

问题


We are trying to follow the "Caching in templates" example listed in http://www.playframework.com/documentation/2.1.1/JavaCache and the compiler throws us an "not enough arguments for method getOrElse" exception message.

Our code in the template:

@play.cache.Cache.getOrElse("cached-content", 3600){
    test
}

So we decided to dig the API and apparently we still short of java.util.concurrent.Callable parameter. Does anyone know what should we pass in for that parameter?

Thanks

Play 2.1.1 Javadoc


回答1:


it's because you are trying to use a Java API from a Scala template with the syntaxe of the Scala cache API. If you want to use the example from the documentation you need to write something like :

@import play.api.Play.current
@play.api.cache.Cache.getOrElse("key", 3600) {
    <h1>Cached content</h1>
} 

the play.api package is the package for Scala APIs.

If you want to use the Java Cache API from a template, this API takes 3 parameters and you need to write something like :

@play.cache.Cache.getOrElse("key", new java.util.concurrent.Callable[String] {
    def call: String = "Cached content again"
}, 3600)


来源:https://stackoverflow.com/questions/16455474/how-do-we-call-caching-in-template-for-play-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!