module ActiveModel::Serializer::Caching

def cache_key(adapter_instance)

def cache_key(adapter_instance)
  return @cache_key if defined?(@cache_key)
  parts = []
  parts << object_cache_key
  parts << adapter_instance.cache_key
  parts << serializer_class._cache_digest unless serializer_class._skip_digest?
  @cache_key = expand_cache_key(parts)
end

def expand_cache_key(parts)

def expand_cache_key(parts)
  ActiveSupport::Cache.expand_cache_key(parts)
end

def fetch(adapter_instance, cache_options = serializer_class._cache_options)

def fetch(adapter_instance, cache_options = serializer_class._cache_options)
  if serializer_class.cache_store
    serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
      yield
    end
  else
    yield
  end
end

def fetch_attributes(fields, cached_attributes, adapter_instance)

## 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
    end
  elsif serializer_class.fragment_cache_enabled?
    fetch_attributes_fragment(adapter_instance, cached_attributes)
  else
    attributes(fields, true)
  end
end

def fetch_attributes_fragment(adapter_instance, cached_attributes = {})

rubocop:disable Metrics/AbcSize
3. Merge the two hashes using adapter_instance#fragment_cache
2. Get non_cached_fields and fetch cache_fields
1. Determine cached fields from serializer class options
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
  fields = serializer_class.fragmented_attributes
  non_cached_fields = fields[:non_cached].dup
  non_cached_hash = attributes(non_cached_fields, true)
  include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
  non_cached_hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
  cached_fields = fields[:cached].dup
  key = cache_key(adapter_instance)
  cached_hash =
    cached_attributes.fetch(key) do
      serializer_class.cache_store.fetch(key, serializer_class._cache_options) 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)
      end
    end
  # Merge both results
  adapter_instance.fragment_cache(cached_hash, non_cached_hash)
end

def object_cache_key

Pass the `key` option to the `cache` declaration or override this method to customize the cache key
Use object's cache_key if available, else derive a key from the object
def object_cache_key
  if object.respond_to?(:cache_key)
    object.cache_key
  elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
    object_time_safe = object.updated_at
    object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
    "#{serializer_cache_key}/#{object.id}-#{object_time_safe}"
  else
    fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{serializer_class}.cache'"
  end
end

def serializer_class

def serializer_class
  @serializer_class ||= self.class
end