-
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.
It's an upgrade based on the new Cache implementation #693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
- Loading branch information
1 parent
a824376
commit ed32db4
Showing
15 changed files
with
363 additions
and
36 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
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,90 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class FragmentCache | ||
|
||
attr_reader :serializer | ||
|
||
def initialize(adapter, serializer, options, root) | ||
@root = root | ||
@options = options | ||
@adapter = adapter | ||
@serializer = serializer | ||
end | ||
|
||
def fetch | ||
klass = serializer.class | ||
# It will split the serializer into two, one that will be cached and other wont | ||
serializers = fragment_serializer(@serializer.object.class.name, klass) | ||
|
||
# Instanciate both serializers | ||
cached_serializer = serializers[:cached].constantize.new(@serializer.object) | ||
non_cached_serializer = serializers[:non_cached].constantize.new(@serializer.object) | ||
|
||
# Get serializable hash from both | ||
cached_hash = @adapter.class.new(cached_serializer, @options).serializable_hash | ||
non_cached_hash = @adapter.class.new(non_cached_serializer, @options).serializable_hash | ||
|
||
# Merge both results | ||
@adapter.fragment_cache(cached_hash, non_cached_hash) | ||
end | ||
|
||
private | ||
|
||
# Add associations to non-cached Serializer | ||
def fragment_associations(serializers, cached_attr, non_cached_attr) | ||
associations = @serializer.each_association | ||
cached_associations = associations.select {|k,v| cached_attr.include?(k)} | ||
non_cached_associations = associations.select {|k,v| !cached_attr.include?(k)} | ||
|
||
# Add cached associations to cached Serializer | ||
cached_associations.each do |k,v| | ||
serializers[:cached].constantize.public_send(v[:type], k, v[:association_options]) | ||
end | ||
|
||
# Add not cached associations to non-cached Serializer | ||
non_cached_associations.each do |k,v| | ||
serializers[:non_cached].constantize.public_send(v[:type], k, v[:association_options]) | ||
end | ||
end | ||
|
||
def cached_attributes_and_association(klass, serializers) | ||
cached_attributes = (klass._cache_only) ? klass._cache_only : @serializer.attributes.keys.delete_if {|attr| klass._cache_except.include?(attr) } | ||
non_cached_attributes = @serializer.attributes.keys.delete_if {|attr| cached_attributes.include?(attr) } | ||
|
||
fragment_associations(serializers, cached_attributes, non_cached_attributes) | ||
|
||
cached_attributes.each do |attribute| | ||
options = @serializer.class._attributes_keys[attribute] | ||
options ||= {} | ||
# Add cached attributes to cached Serializer | ||
serializers[:cached].constantize.attribute(attribute, options) | ||
end | ||
|
||
non_cached_attributes.each do |attribute| | ||
options = @serializer.class._attributes_keys[attribute] | ||
options ||= {} | ||
# Add non-cached attributes to non-cached Serializer | ||
serializers[:non_cached].constantize.attribute(attribute, options) | ||
end | ||
end | ||
|
||
def fragment_serializer(name, klass) | ||
cached = "#{name.capitalize}CachedSerializer" | ||
non_cached = "#{name.capitalize}NonCachedSerializer" | ||
|
||
Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached) | ||
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached) | ||
|
||
klass._cache_options ||= {} | ||
klass._cache_options[:key] = klass._cache_key if klass._cache_key | ||
cached.constantize.cache(klass._cache_options) | ||
|
||
serializers = {cached: cached, non_cached: non_cached} | ||
cached_attributes_and_association(klass, serializers) | ||
serializers | ||
end | ||
end | ||
end | ||
end | ||
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
Binary file not shown.
15 changes: 15 additions & 0 deletions
15
lib/active_model/serializer/adapter/json/fragment_cache.rb
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,15 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class Json < Adapter | ||
class FragmentCache | ||
|
||
def fragment_cache(cached_hash, non_cached_hash) | ||
non_cached_hash.merge cached_hash | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
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
26 changes: 26 additions & 0 deletions
26
lib/active_model/serializer/adapter/json_api/fragment_cache.rb
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,26 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class JsonApi < Adapter | ||
class FragmentCache | ||
|
||
def fragment_cache(root, cached_hash, non_cached_hash) | ||
if root | ||
hash = {} | ||
core_cached = cached_hash.first | ||
core_non_cached = non_cached_hash.first | ||
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] } | ||
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] } | ||
|
||
cached_resource = (core_cached[1]) ? core_cached[1].merge(core_non_cached[1]) : core_non_cached[1] | ||
hash = (root) ? { root => cached_resource } : cached_resource | ||
|
||
hash.merge no_root_non_cache.merge no_root_cache | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
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
Oops, something went wrong.