Adding cache support to version 0.10.0

It's a new implementation of cache based on ActiveSupport::Cache.
The implementation abstracts the cache in Adapter class on a
private method called cached_object, this method is intended
to be used on Adapters inside serializable_hash method in order
to cache each instance of the object that will be returned by
the serializer.

Some of its features are:
- A different syntax. (no longer need the cache_key method).
- An options argument that have the same arguments of ActiveSupport::Cache::Store, plus a key option that will be the prefix of the object cache on a pattern "#{key}-#{object.id}".
- It cache the objects individually and not the whole Serializer return, re-using it in different requests (as a show and a index method for example.)
This commit is contained in:
Joao Moura
2014-10-20 21:38:20 -02:00
committed by João Moura
parent 42644544e1
commit 8a432ad2b3
17 changed files with 265 additions and 22 deletions

View File

@@ -10,6 +10,9 @@ module ActiveModel
attr_accessor :_attributes
attr_accessor :_associations
attr_accessor :_urls
attr_accessor :_cache
attr_accessor :_cache_key
attr_accessor :_cache_options
end
def self.inherited(base)
@@ -36,7 +39,14 @@ module ActiveModel
end unless method_defined?(key)
end
# Defines an association in the object that should be rendered.
# Enables a serializer to be automatically cached
def self.cache(options = {})
@_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
@_cache_key = options.delete(:key)
@_cache_options = (options.empty?) ? nil : options
end
# Defines an association in the object should be rendered.
#
# The serializer object should implement the association name
# as a method which should return an array when invoked. If a method

View File

@@ -50,6 +50,20 @@ module ActiveModel
json[meta_key] = meta if meta && root
json
end
private
def cached_object
klass = serializer.class
if klass._cache
_cache_key = (klass._cache_key) ? "#{klass._cache_key}/#{serializer.object.id}-#{serializer.object.updated_at}" : serializer.object.cache_key
klass._cache.fetch(_cache_key, klass._cache_options) do
yield
end
else
yield
end
end
end
end
end

View File

@@ -6,19 +6,21 @@ module ActiveModel
if serializer.respond_to?(:each)
@result = serializer.map{|s| self.class.new(s).serializable_hash }
else
@result = serializer.attributes(options)
serializer.each_association do |name, association, opts|
if association.respond_to?(:each)
array_serializer = association
@result[name] = array_serializer.map { |item| item.attributes(opts) }
else
if association
@result[name] = association.attributes(options)
@result = cached_object do
@hash = serializer.attributes(options)
serializer.each_association do |name, association, opts|
if association.respond_to?(:each)
array_serializer = association
@hash[name] = array_serializer.map { |item| item.attributes(opts) }
else
@result[name] = nil
if association
@hash[name] = association.attributes(options)
else
@hash[name] = nil
end
end
end
@hash
end
end

View File

@@ -23,10 +23,12 @@ module ActiveModel
self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[@root]
end
else
@hash[@root] = attributes_for_serializer(serializer, @options)
add_resource_links(@hash[@root], serializer)
@hash = cached_object do
@hash[@root] = attributes_for_serializer(serializer, @options)
add_resource_links(@hash[@root], serializer)
@hash
end
end
@hash
end

View File

@@ -1,7 +1,7 @@
require "active_model"
require "active_model/serializer/version"
require "active_model/serializer"
require "active_model/serializer/fieldset"
require 'active_model'
require 'active_model/serializer/version'
require 'active_model/serializer'
require 'active_model/serializer/fieldset'
begin
require 'action_controller'