What is the correct way to cache the result of future in playframework. E.g.:
val userGravatar: Future[JsValue] = RemoteGravatarService.get(user.id, user.email)
object RemoveGravatarService {
def get(userId: String, email: String): Future[JsValue] = {
Cache.getOrElse("gravatar-for-$userId", 1.hour) {
WS.url("gravatar.com/email=$email").get().asJson
}
}
}
We don't want to ask (this fictional) "Gravatar" every time, because it doesn't change that often. But we need som userGravatar info quite often locally.
Here we cache the future itself, but actually we only want to cache the result of the future. Is there a convenient/correct way to do this in play?
JsValue
(since it's serializeable). Updated example with type annotations. The important thing here is that I want the cache to respond withFuture.successful(previousJsonResult)
if the value is ready in cache. – Kaila