Class: CacheCrispies::Plan
- Inherits:
-
Object
- Object
- CacheCrispies::Plan
- 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
-
#cacheable ⇒ Object
readonly
Returns the value of attribute cacheable.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
-
#cache { ... } ⇒ Object
Caches the contents of the block, if the plan is cacheable, otherwise calls yields to the block directly.
-
#cache_key ⇒ String
Returns a string of cache keys for all dependent objects.
-
#collection? ⇒ Boolean
Whether or not the cacheable should be treated like a collection.
-
#etag ⇒ String
Returns the cache_key in a format suitable for an ETag header.
-
#initialize(serializer, cacheable, key: UNDEFINED, collection: nil, **options) ⇒ Plan
constructor
Initializes a new instance of CacheCrispies::Plan.
-
#wrap(json_hash) ⇒ Hash, Object
Wraps a value in a JSON key/object.
Constructor Details
#initialize(serializer, cacheable, key: UNDEFINED, collection: nil, **options) ⇒ Plan
Initializes a new instance of CacheCrispies::Plan
19 20 21 22 23 24 25 26 |
# File 'lib/cache_crispies/plan.rb', line 19 def initialize(serializer, cacheable, key: UNDEFINED, collection: nil, **) @serializer = serializer @cacheable = cacheable @key = key @collection = collection @options = end |
Instance Attribute Details
#cacheable ⇒ Object (readonly)
Returns the value of attribute cacheable.
6 7 8 |
# File 'lib/cache_crispies/plan.rb', line 6 def cacheable @cacheable end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/cache_crispies/plan.rb', line 6 def @options end |
#serializer ⇒ Object (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
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_key ⇒ String
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)
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
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 |
#etag ⇒ String
Returns the cache_key in a format suitable for an ETag header
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.
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 |