mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Merge pull request #344 from tjschuck/caching_docs
Add docs about caching
This commit is contained in:
commit
82cc4e815a
18
README.md
18
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
|
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
|
`serialization_scope` to `nil`, the `index` action no longer will need to make
|
||||||
that query, only the `show` action will.
|
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.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user