Provide caching options for the result of your Granola serializers.
class PersonSerializer < Granola::Serializer
cache key: "person", expire_in: 3600
def data
{
id: object.id,
name: object.name,
}
end
def cache_key
"%s:%s" % [object.id, object.updated_at.to_i]
end
end
serializer = PersonSerializer.new(person)
serializer.to_json # Generates JSON and stores it in the cache.
serializer.to_json # Retreives from cache without rendering.
person.update(...) # Do something that would change the `cache_key` (e.g. update
# the object's `updated_at`.)
serializer.to_json # Generates JSON again, storing the new version in the cache.
NOTE: Changing the cache key will invalidate previous versions of the cached object, but will not delete them from the cache store.
gem install granola-cache
By default, Granola::Cache stores cached output from serializers in an in-memory Hash. This is not meant for production use. You should provide an alternative store for your application.
Alternative stores should implement a single method:
fetch(key, options = {}, &block)
Where:
key
: The cache key to fetch from cache.options
: Any options the cache store can take (for example,:expire_in
if your store supports Time-based expiration.)
If the key isn't found in the store, the block is invoked, and the result from this block is both returned and stored in the cache for further use.
There's an example Redis Store included in this repository, should you wish to inspect it.
This is compatible with ActiveSupport::Cache::Store
, so if you're in a Rails
application, you can just do this in config/initializers/granola.rb
:
Granola::Cache.store = Rails.cache
Pass caching options to a serializer by calling the cache
singleton method.
Granola::Cache only recognizes these options: :key
and :store
. Any other
option passed will be ignored by Granola, but forwarded to the cache
store.
This is meant to be a prefix applied to cache keys. In the example above, any particular serializer will be stored in the cache with the following key:
"#{key}/#{object.id}:#{object.updated_at.to_i}"
This allows overriding the caching store for a specific serializer.
class WeeklyReportSerializer < Granola::Serializer
cache store: DifferentStore.new
end
See Cache Stores for more on configuring the global store.
This project is shared under the MIT license. See the attached LICENSE file for details.