Class: CacheCrispies::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_crispies/plan.rb

Overview

Represents a plan on how to cache a given cacheable with a given serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, cacheable, key: UNDEFINED, collection: nil, **options) ⇒ Plan

Initializes a new instance of CacheCrispies::Plan

Parameters:

  • serializer (CacheCrispies::Base)

    a class inheriting from CacheCrispies::Base

  • cacheable (Object)

    typically ActiveRecord::Base or an enumerable containing instances of ActiveRecord::Base, but could be anything

  • options (Hash)

    any optional values from the serializer instance

Options Hash (**options):

  • :key (Symbol)

    the name of the root key to nest the JSON data under

  • :collection (Boolean)

    whether to render the data as a collection/array or a single object



19
20
21
22
23
24
25
26
# File 'lib/cache_crispies/plan.rb', line 19

def initialize(serializer, cacheable, key: UNDEFINED, collection: nil, **options)
  @serializer = serializer
  @cacheable = cacheable

  @key = key
  @collection = collection
  @options = options
end

Instance Attribute Details

#cacheableObject (readonly)

Returns the value of attribute cacheable.



6
7
8
# File 'lib/cache_crispies/plan.rb', line 6

def cacheable
  @cacheable
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/cache_crispies/plan.rb', line 6

def options
  @options
end

#serializerObject (readonly)

Returns the value of attribute serializer.



6
7
8
# File 'lib/cache_crispies/plan.rb', line 6

def serializer
  @serializer
end

Instance Method Details

#cache { ... } ⇒ Object

Caches the contents of the block, if the plan is cacheable, otherwise calls yields to the block directly

Yields:

  • calls the block that should return a value to be cached

Returns:

  • whatever the provided block returns



70
71
72
73
74
75
76
# File 'lib/cache_crispies/plan.rb', line 70

def cache
  if cache?
    CacheCrispies.cache.fetch(cache_key) { yield }
  else
    yield
  end
end

#cache_keyString

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)

Returns:

  • (String)

    a suitable cache key



54
55
56
57
58
59
60
61
62
63
# 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

#collection?Boolean

Whether or not the cacheable should be treated like a collection

Returns:

  • (Boolean)

    true if cacheable is a collection



31
32
33
34
35
# File 'lib/cache_crispies/plan.rb', line 31

def collection?
  return @collection unless @collection.nil?

  @collection = cacheable.respond_to?(:each)
end

#etagString

Returns the cache_key in a format suitable for an ETag header

Returns:

  • (String)

    an MD5 digest of cache_key



40
41
42
# File 'lib/cache_crispies/plan.rb', line 40

def etag
  Digest::MD5.hexdigest(cache_key)
end

#wrap(json_hash) ⇒ Hash, Object

Wraps a value in a JSON key/object. Returns json_hash directly if there is no key.

Parameters:

  • json_hash (Hash, Array, Object)

    typically a JSON-ready Hash or Array, but could be anything really

Returns:

  • (Hash, Object)

    will return a hash with a single key of #key, unless there is no #key, then returns the json_hash directly.



85
86
87
88
89
# File 'lib/cache_crispies/plan.rb', line 85

def wrap(json_hash)
  return json_hash unless key?

  { key => json_hash }
end