From 027aa38138b92e86d07a13753236595948830a76 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Wed, 26 Jun 2013 16:45:50 -0400 Subject: [PATCH] Add docs about caching --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 00880659..62b64fea 100644 --- a/README.md +++ b/README.md @@ -675,3 +675,21 @@ Assuming that the `current_admin` method needs to make a query in the database for the current user, the advantage of this approach is that, by setting `serialization_scope` to `nil`, the `index` action no longer will need to make that query, only the `show` action will. + +## Caching + +To cache a serializer, call `cached` and define a `cache_key` method: + +```ruby +class PostSerializer < ActiveModel::Serializer + cached # enables caching for this serializer + + attributes :title, :body + + def cache_key + [object, current_user] + end +end +``` + +The caching interface uses `Rails.cache` under the hood.