The Play cache API
Add cache into your dependencies list. For example, in build.sbt:
将cache加入到你的关联列表中。例如,在build.sbt中:
play.api.cache.Cache对象提供了缓存API.其需要一个注册的缓存插件。
使用这个简单的API你既可以存储数据于缓存中:
Cache.set("item.key", connectedUser)
当其不存在时候这里还有一个简洁的帮助器去从缓存中获取或在缓存中设定值:
val user: User = Cache.getOrElse[User]("item.key") {
User.findById(connectedUser)
}
To remove an item from the cache use the remove method:
使用remove方法去从缓存中移除一个条目:
Cache.remove("item.key")
你可以使用标准的Action组合。
def index = Cached("homePage") {
Ok("Hello world")
}
}
甚至于:
user =>
Cached(req => "profile." + user) {
Action {
Ok(views.html.profile(User.find(user)))
}
}
}
你可以简单的控制什么是你想缓存的或者什么是你不想缓存的。
你可能仅需要结果为“缓存200 OK”
def get(index: Int) = {
.status(_ => "/resource/"+ index, 200)
caching {
Action {
if (index % 2 == 1) {
Ok(Json.obj("id" -> index))
} else {
NotFound
}
}
}