§The Play cache API
The default implementation of the Cache API uses EHCache. You can also provide your own implementation via a plug-in.
§Importing the Cache API
Add cache
into your dependencies list. For example, in build.sbt
:
libraryDependencies ++= Seq(
cache,
...
)
§Accessing the Cache API
The cache API is provided by the play.api.cache.Cache
object. It requires a registered cache plug-in.
Note: The API is intentionally minimal to allow several implementation to be plugged. If you need a more specific API, use the one provided by your Cache plugin.
Using this simple API you can either store data in cache:
Cache.set("item.key", connectedUser)
And then retrieve it later:
val maybeUser: Option[User] = Cache.getAs[User]("item.key")
There is also a convenient helper to retrieve from cache or set the value in cache if it was missing:
val user: User = Cache.getOrElse[User]("item.key") {
User.findById(connectedUser)
}
To remove an item from the cache use the remove
method:
Cache.remove("item.key")
§Caching HTTP responses
You can easily create smart cached actions using standard Action composition.
Note: Play HTTP
Result
instances are safe to cache and reuse later.
Play provides a default built-in helper for standard cases:
def index = Cached("homePage") {
Action {
Ok("Hello world")
}
}
Or even:
def userProfile = Authenticated {
user =>
Cached(req => "profile." + user) {
Action {
Ok(views.html.profile(User.find(user)))
}
}
}
§Control caching
You can easily control what you want to cache or what you want to exclude from the cache.
You may want to only cache 200 Ok results.
def get(index: Int) = Cached.status(_ => "/resource/"+ index, 200) {
Action {
if (index > 0) {
Ok(Json.obj("id" -> index))
} else {
NotFound
}
}
}
Or cache 404 Not Found only for a couple of minutes
def get(index: Int) = {
val caching = Cached
.status(_ => "/resource/"+ index, 200)
.includeStatus(404, 600)
caching {
Action {
if (index % 2 == 1) {
Ok(Json.obj("id" -> index))
} else {
NotFound
}
}
}
}
Next: Calling web services