Module: Garner::Cache::ObjectIdentity
- Defined in:
- lib/garner/cache/object_identity.rb
Overview
A cache that uses an object identity binding strategy.
Allows some flexibility in how caller binds objects in cache. The binding can be an object, class, array of objects, or array of classes on which to bind the validity of the cached result contained in the subsequent block.
invalidated on any change to the ‘Widget` object whose slug attribute equals `params`
invalidated on any change to the ‘User` object whose id attribute equals current_user.id. This is one way to bind a cache result to any change in the current user.
any object of class Widget. This is the appropriate strategy for index paths like /widgets.
cached instance to be invalidated on any change to either the current user, or any object of class Widget.
‘bind: [{ klass: Artwork }, { klass: User, object: { id: current_user.id } }]`
Constant Summary collapse
- IDENTITY_FIELDS =
[ :id ]
- KEY_STRATEGIES =
[ Garner::Strategies::Keys::Caller, Garner::Strategies::Keys::Version, Garner::Strategies::Keys::RequestPath, Garner::Strategies::Keys::RequestGet ]
- CACHE_STRATEGIES =
[ Garner::Strategies::Cache::Expiration ]
- ETAG_STRATEGY =
Garner::Strategies::ETags::Grape
Class Method Summary collapse
-
.cache(binding = nil, context = {}) ⇒ Object
cache the result of an executable block.
-
.cache_metadata(binding, context = {}) ⇒ Object
metadata for cached objects: :etag - Unique hash of object content :last_modified - Timestamp of last modification event.
-
.invalidate(*args) ⇒ Object
invalidate an object that has been cached.
Class Method Details
.cache(binding = nil, context = {}) ⇒ Object
cache the result of an executable block
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/garner/cache/object_identity.rb', line 53 def cache(binding = nil, context = {}) # apply cache strategies = (context) CACHE_STRATEGIES.each do |strategy| = strategy.apply() end key = key(binding, key_context(context)) result = Garner.config.cache.fetch(key, ) do object = yield (key, object) object end Garner.config.cache.delete(key) unless result result end |
.cache_metadata(binding, context = {}) ⇒ Object
metadata for cached objects:
:etag - Unique hash of object content
:last_modified - Timestamp of last modification event
79 80 81 82 |
# File 'lib/garner/cache/object_identity.rb', line 79 def (binding, context = {}) key = key(binding, key_context(context)) Garner.config.cache.read((key)) end |
.invalidate(*args) ⇒ Object
invalidate an object that has been cached
70 71 72 73 74 |
# File 'lib/garner/cache/object_identity.rb', line 70 def invalidate(* args) = index(*args) reset_key_prefix_for([:klass], [:object]) reset_key_prefix_for([:klass]) if [:object] end |