-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support read_multi #1372
support read_multi #1372
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ class Attributes < Base | |
def initialize(serializer, options = {}) | ||
super | ||
@include_tree = IncludeTree.from_include_args(options[:include] || '*') | ||
@cached_attributes = options[:cache_attributes] || {} | ||
end | ||
|
||
def serializable_hash(options = nil) | ||
|
@@ -24,9 +25,38 @@ def fragment_cache(cached_hash, non_cached_hash) | |
private | ||
|
||
def serializable_hash_for_collection(options) | ||
cache_attributes | ||
|
||
serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) } | ||
end | ||
|
||
# Read cache from cache_store | ||
# @return [Hash] | ||
def cache_read_multi | ||
return {} if ActiveModelSerializers.config.cache_store.blank? | ||
|
||
keys = CachedSerializer.object_cache_keys(serializer, @include_tree) | ||
|
||
return {} if keys.blank? | ||
|
||
ActiveModelSerializers.config.cache_store.read_multi(*keys) | ||
end | ||
|
||
# Set @cached_attributes | ||
def cache_attributes | ||
return if @cached_attributes.present? | ||
|
||
@cached_attributes = cache_read_multi | ||
end | ||
|
||
# Get attributes from @cached_attributes | ||
# @return [Hash] cached attributes | ||
def cached_attributes(cached_serializer) | ||
return yield unless cached_serializer.cached? | ||
|
||
@cached_attributes.fetch(cached_serializer.cache_key) { yield } | ||
end | ||
|
||
def serializable_hash_for_single_resource(options) | ||
resource = resource_object_for(options) | ||
relationships = resource_relationships(options) | ||
|
@@ -56,8 +86,12 @@ def include_meta(json) | |
end | ||
|
||
def resource_object_for(options) | ||
cache_check(serializer) do | ||
serializer.attributes(options[:fields]) | ||
cached_serializer = CachedSerializer.new(serializer) | ||
|
||
cached_attributes(cached_serializer) do | ||
cached_serializer.cache_check(self) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bf4 we still use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it the switch from |
||
serializer.attributes(options[:fields]) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't recall why we removed the |
||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this both blocks of code added in this diff should be their own method. Maybe something like (pardon the naming... could be better)
def resource_object_for(options)