-
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
4891c22
commit b8c5af4
Showing
19 changed files
with
555 additions
and
102 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
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,78 @@ | ||
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) | ||
|
||
cached_adapter = @adapter.class.new(cached_serializer, @options) | ||
non_cached_adapter = @adapter.class.new(non_cached_serializer, @options) | ||
|
||
# Get serializable hash from both | ||
cached_hash = cached_adapter.serializable_hash | ||
non_cached_hash = non_cached_adapter.serializable_hash | ||
|
||
# Merge both results | ||
@adapter.fragment_cache(cached_hash, non_cached_hash) | ||
end | ||
|
||
private | ||
|
||
def cached_attributes(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) } | ||
|
||
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) | ||
|
||
cached.constantize.fragmented(serializer) | ||
non_cached.constantize.fragmented(serializer) | ||
|
||
serializers = {cached: cached, non_cached: non_cached} | ||
cached_attributes(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
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 |
Oops, something went wrong.