Merge pull request #89 from twinturbo/caching

Serializers Cache JSON & Hashes
This commit is contained in:
Steve Klabnik
2013-03-15 10:30:04 -07:00
4 changed files with 202 additions and 24 deletions

View File

@@ -19,26 +19,18 @@ module ActiveModel
class_attribute :root
def initialize(object, options={})
@object, @options = object, options
class_attribute :cache
class_attribute :perform_caching
class << self
# set peform caching like root
def cached(value = true)
self.perform_caching = value
end
end
def serializable_array
@object.map do |item|
if @options.has_key? :each_serializer
serializer = @options[:each_serializer]
elsif item.respond_to?(:active_model_serializer)
serializer = item.active_model_serializer
end
serializable = serializer ? serializer.new(item, @options) : DefaultSerializer.new(item)
if serializable.respond_to?(:serializable_hash)
serializable.serializable_hash
else
serializable.as_json
end
end
def initialize(object, options={})
@object, @options = object, options
end
def meta_key
@@ -61,6 +53,52 @@ module ActiveModel
serializable_array
end
end
end
def to_json(*args)
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
super
end
else
super
end
end
def serializable_array
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-array']) do
_serializable_array
end
else
_serializable_array
end
end
private
def _serializable_array
@object.map do |item|
if @options.has_key? :each_serializer
serializer = @options[:each_serializer]
elsif item.respond_to?(:active_model_serializer)
serializer = item.active_model_serializer
end
serializable = serializer ? serializer.new(item, @options) : DefaultSerializer.new(item)
if serializable.respond_to?(:serializable_hash)
serializable.serializable_hash
else
serializable.as_json
end
end
end
def expand_cache_key(*args)
ActiveSupport::Cache.expand_cache_key(args)
end
def perform_caching?
perform_caching && cache && respond_to?(:cache_key)
end
end
end

View File

@@ -69,7 +69,15 @@ module ActiveModel
class_attribute :use_default_render_json
self.use_default_render_json = false
class_attribute :cache
class_attribute :perform_caching
class << self
# set peform caching like root
def cached(value = true)
self.perform_caching = value
end
# Define attributes to be used in the serialization.
def attributes(*attrs)
@@ -266,6 +274,16 @@ module ActiveModel
hash[meta_key] = @options[:meta] if @options.has_key?(:meta)
end
def to_json(*args)
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
super
end
else
super
end
end
# Returns a json representation of the serializable
# object including the root.
def as_json(options={})
@@ -284,10 +302,13 @@ module ActiveModel
# Returns a hash representation of the serializable
# object without the root.
def serializable_hash
return nil if @object.nil?
@node = attributes
include_associations! if _embed
@node
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do
_serializable_hash
end
else
_serializable_hash
end
end
def include_associations!
@@ -400,6 +421,21 @@ module ActiveModel
alias :read_attribute_for_serialization :send
def _serializable_hash
return nil if @object.nil?
@node = attributes
include_associations! if _embed
@node
end
def perform_caching?
perform_caching && cache && respond_to?(:cache_key)
end
def expand_cache_key(*args)
ActiveSupport::Cache.expand_cache_key(args)
end
# Use ActiveSupport::Notifications to send events to external systems.
# The event name is: name.class_name.serializer
def instrument(name, payload = {}, &block)
@@ -407,7 +443,7 @@ module ActiveModel
ActiveSupport::Notifications.instrument(event_name, payload, &block)
end
end
# DefaultSerializer
#
# Provides a constant interface for all items, particularly

View File

@@ -23,6 +23,14 @@ if defined?(Rails)
include app.routes.url_helpers
end
end
initializer "caching.active_model_serializer" do |app|
ActiveModel::Serializer.perform_caching = app.config.action_controller.perform_caching
ActiveModel::ArraySerializer.perform_caching = app.config.action_controller.perform_caching
ActiveModel::Serializer.cache = Rails.cache
ActiveModel::ArraySerializer.cache = Rails.cache
end
end
end
end