You are viewing the documentation for the 2.2.x release series. The latest stable release series is 3.0.x.

§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(

§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") {

To remove an item from the cache use the remove method:


§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 {

Next: Calling web services

Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.