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, 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, options = {}) ⇒ Plan
Initializes a new instance of CacheCrispies::Plan
19 20 21 22 23 24 25 26 27 |
# File 'lib/cache_crispies/plan.rb', line 19 def initialize(serializer, cacheable, = {}) @serializer = serializer @cacheable = cacheable opts = .dup @key = opts.delete(:key) @collection = opts.delete(:collection) @options = opts 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? Rails.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)
55 56 57 58 59 60 61 62 63 |
# File 'lib/cache_crispies/plan.rb', line 55 def cache_key @cache_key ||= [ CACHE_KEY_PREFIX, serializer.cache_key_base, 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
32 33 34 35 36 |
# File 'lib/cache_crispies/plan.rb', line 32 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
41 42 43 |
# File 'lib/cache_crispies/plan.rb', line 41 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 |