-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOCS] Refactor, update, create documentation [ci skip]
- Loading branch information
Showing
19 changed files
with
543 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Caching | ||
|
||
To cache a serializer, call ```cache``` and pass its options. | ||
The options are the same options of ```ActiveSupport::Cache::Store```, plus | ||
a ```key``` option that will be the prefix of the object cache | ||
on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```. | ||
|
||
The cache support is optimized to use the cached object in multiple request. An object cached on a ```show``` request will be reused at the ```index```. If there is a relationship with another cached serializer it will also be created and reused automatically. | ||
|
||
**[NOTE] Every object is individually cached.** | ||
|
||
**[NOTE] The cache is automatically expired after an object is updated, but it's not deleted.** | ||
|
||
```ruby | ||
cache(options = nil) # options: ```{key, expires_in, compress, force, race_condition_ttl}``` | ||
``` | ||
|
||
Take the example bellow: | ||
|
||
```ruby | ||
class PostSerializer < ActiveModel::Serializer | ||
cache key: 'post', expires_in: 3.hours | ||
attributes :title, :body | ||
|
||
has_many :comments | ||
end | ||
``` | ||
|
||
On this example every ```Post``` object will be cached with | ||
the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want, | ||
but in this case it will be automatically expired after 3 hours. | ||
|
||
## Fragment Caching | ||
|
||
If there is some API endpoint that shouldn't be fully cached, you can still optimise it, using Fragment Cache on the attributes and relationships that you want to cache. | ||
|
||
You can define the attribute by using ```only``` or ```except``` option on cache method. | ||
|
||
**[NOTE] Cache serializers will be used at their relationships** | ||
|
||
Example: | ||
|
||
```ruby | ||
class PostSerializer < ActiveModel::Serializer | ||
cache key: 'post', expires_in: 3.hours, only: [:title] | ||
attributes :title, :body | ||
|
||
has_many :comments | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.