mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Move attributes cache method out of concern
This commit is contained in:
@@ -170,6 +170,7 @@ module ActiveModel
|
||||
|
||||
# Read cache from cache_store
|
||||
# @return [Hash]
|
||||
# Used in CollectionSerializer to set :cached_attributes
|
||||
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
|
||||
return {} if ActiveModelSerializers.config.cache_store.blank?
|
||||
|
||||
@@ -215,23 +216,17 @@ module ActiveModel
|
||||
|
||||
### INSTANCE METHODS
|
||||
def fetch_attributes(fields, cached_attributes, adapter_instance)
|
||||
if serializer_class.cache_enabled?
|
||||
key = cache_key(adapter_instance)
|
||||
cached_attributes.fetch(key) do
|
||||
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
||||
attributes(fields, true)
|
||||
end
|
||||
key = cache_key(adapter_instance)
|
||||
cached_attributes.fetch(key) do
|
||||
fetch(adapter_instance, serializer_class._cache_options, key) do
|
||||
attributes(fields, true)
|
||||
end
|
||||
elsif serializer_class.fragment_cache_enabled?
|
||||
fetch_attributes_fragment(adapter_instance, cached_attributes)
|
||||
else
|
||||
attributes(fields, true)
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(adapter_instance, cache_options = serializer_class._cache_options)
|
||||
def fetch(adapter_instance, cache_options = serializer_class._cache_options, key = cache_key(adapter_instance))
|
||||
if serializer_class.cache_store
|
||||
serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
|
||||
serializer_class.cache_store.fetch(key, cache_options) do
|
||||
yield
|
||||
end
|
||||
else
|
||||
@@ -242,7 +237,6 @@ module ActiveModel
|
||||
# 1. Determine cached fields from serializer class options
|
||||
# 2. Get non_cached_fields and fetch cache_fields
|
||||
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
|
||||
serializer_class._cache_options ||= {}
|
||||
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
|
||||
@@ -257,7 +251,7 @@ module ActiveModel
|
||||
key = cache_key(adapter_instance)
|
||||
cached_hash =
|
||||
cached_attributes.fetch(key) do
|
||||
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
||||
fetch(adapter_instance, serializer_class._cache_options, key) do
|
||||
hash = attributes(cached_fields, true)
|
||||
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
||||
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
||||
@@ -266,7 +260,6 @@ module ActiveModel
|
||||
# Merge both results
|
||||
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
|
||||
end
|
||||
# rubocop:enable Metrics/AbcSize
|
||||
|
||||
def cache_key(adapter_instance)
|
||||
return @cache_key if defined?(@cache_key)
|
||||
|
||||
Reference in New Issue
Block a user