Playframework [Java]: what replaces @play.api.cache.Cache.getOrElse when migrating from Play 2.5 to 2.6

302 Views Asked by At

I hava a Play 2.5 Java project which has a bunch of templates (i.e. *.scala.html files) associated with it. I am attempting to upgrade the project from 2.5 to 2.6 but the project no longer compiles, the offending code segments typically look like this

@(user: models.User)  
@import play.api.Play.current  
@play.api.cache.Cache.getOrElse("header" + user.getId(), 3600) {  
    ...  
}  

and the error trace is

[error] head.scala.html:3: object Cache is not a member of package play.api.cache  
[error] @play.api.cache.Cache.getOrElse("header" + user.getId(), 3600) {  
[error]                 ^  

the Play documentation suggests using 'play.cache.SyncCacheApi' instead i.e. https://www.playframework.com/documentation/2.6.x/CacheMigration26
but this API does NOT include a getOrElse() equivalent function, the closest one that I could find was
T getOrElseUpdate(String key, Callable block, int expiration);
which I can't figure out how to use within a template.

Another part of the Play documentation suggests that I use Injectable Twirl Templates i.e. https://www.playframework.com/documentation/2.6.x/Highlights26#Injectable-Twirl-Templates
which is even more confusing and it still doesn't address what repalcement function do I use for getOrElse in my templates.

If anyone has a working example of a replacement function for play.api.cache.Cache.getOrElse() that would be a great help.

0

There are 0 best solutions below