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

2019-07-27 02:27发布

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条回答
可以哭但决不认输i
2楼-- · 2019-07-27 02:54

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)
查看更多
登录 后发表回答