class CacheCrispies::Plan
Represents a plan on how to cache a given cacheable with a given serializer
Attributes
Public Class Methods
Initializes a new instance of CacheCrispies::Plan
@param serializer [CacheCrispies::Base] a class inheriting from
CacheCrispies::Base
@param cacheable [Object] typically ActiveRecord::Base or an enumerable
containing instances of ActiveRecord::Base, but could be anything
@param [Hash] options any optional values from the serializer instance @option options [Symbol] :key the name of the root key to nest the JSON
data under
@option options [Boolean] :collection whether to render the data as a
collection/array or a single object
# File lib/cache_crispies/plan.rb, line 19 def initialize(serializer, cacheable, key: nil, collection: nil, **options) @serializer = serializer @cacheable = cacheable @key = key @collection = collection @options = options end
Public Instance Methods
Caches the contents of the block, if the plan is cacheable, otherwise calls yields to the block directly
@yield calls the block that should return a value to be cached @return whatever the provided block returns
# File lib/cache_crispies/plan.rb, line 70 def cache if cache? CacheCrispies.cache.fetch(cache_key) { yield } else yield end end
Returns a string of cache keys for all dependent objects. Changes to any of keys should bust the overall key for this plan. The key consists of:
-
a global key for this gem
-
the serializers class name
-
a digest of the contents of the of serializer class file
-
any addon keys the serializer may define
-
the
cache_key
method on the cacheable (ActiveRecord provides this by default)
@return [String] a suitable cache key
# File lib/cache_crispies/plan.rb, line 54 def cache_key @cache_key ||= [ CACHE_KEY_PREFIX, serializer.cache_key_base, serializer.dependency_key, addons_key, cacheable_cache_key ].flatten.compact.join(CACHE_KEY_SEPARATOR) end
Whether or not the cacheable should be treated like a collection
@return [Boolean] true if cacheable is a collection
# File lib/cache_crispies/plan.rb, line 31 def collection? return @collection unless @collection.nil? @collection = cacheable.respond_to?(:each) end
Wraps a value in a JSON key/object. Returns json_hash directly if there is no key.
@param json_hash [Hash, Array, Object] typically a JSON-ready Hash or
Array, but could be anything really
@return [Hash, Object] will return a hash with a single key of key
,
unless there is no #key, then returns the json_hash directly.
# File lib/cache_crispies/plan.rb, line 85 def wrap(json_hash) return json_hash unless key? { key => json_hash } end
Private Instance Methods
# File lib/cache_crispies/plan.rb, line 111 def addons_key addons = serializer.cache_key_addons(options) return nil if addons.compact.empty? Digest::MD5.hexdigest(addons.join('|')) end
# File lib/cache_crispies/plan.rb, line 103 def cache? serializer.do_caching? && cacheable.respond_to?(CacheCrispies.config.cache_key_method) end
# File lib/cache_crispies/plan.rb, line 107 def cacheable_cache_key cacheable.public_send(CacheCrispies.config.cache_key_method) end
# File lib/cache_crispies/plan.rb, line 93 def key return @key unless @key.nil? (collection? ? serializer.collection_key : serializer.key) end
# File lib/cache_crispies/plan.rb, line 99 def key? !!key end