mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 14:29:31 +00:00
Breaking change: - Adapters now inherit Adapter::Base - 'Adapter' is now a module, no longer a class Why? - using a class as a namespace that you also inherit from is complicated and circular at time i.e. buggy (see https://github.com/rails-api/active_model_serializers/pull/1177) - The class methods on Adapter aren't necessarily related to the instance methods, they're more Adapter functions - named `Base` because it's a Rails-ism - It helps to isolate and highlight what the Adapter interface actually is
46 lines
1.4 KiB
Ruby
46 lines
1.4 KiB
Ruby
module ActiveModel
|
|
class Serializer
|
|
module Adapter
|
|
class CachedSerializer
|
|
def initialize(serializer)
|
|
@cached_serializer = serializer
|
|
@klass = @cached_serializer.class
|
|
end
|
|
|
|
def cache_check(adapter_instance)
|
|
if cached?
|
|
@klass._cache.fetch(cache_key, @klass._cache_options) do
|
|
yield
|
|
end
|
|
elsif fragment_cached?
|
|
FragmentCache.new(adapter_instance, @cached_serializer, adapter_instance.instance_options).fetch
|
|
else
|
|
yield
|
|
end
|
|
end
|
|
|
|
def cached?
|
|
@klass._cache && !@klass._cache_only && !@klass._cache_except
|
|
end
|
|
|
|
def fragment_cached?
|
|
@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except
|
|
end
|
|
|
|
def cache_key
|
|
parts = []
|
|
parts << object_cache_key
|
|
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
|
|
parts.join('/')
|
|
end
|
|
|
|
def object_cache_key
|
|
object_time_safe = @cached_serializer.object.updated_at
|
|
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
|
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|