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 } }]`

Examples:

‘bind: { klass: Widget, object: { id: params } }` will cause a cached instance to be

‘bind: { klass: User, object: { id: current_user.id } }` will cause a cached instance to be

‘bind: { klass: Widget }` will cause the cached instance to be invalidated on any change to

‘bind: [{ klass: Widget }, { klass: User, object: { id: current_user.id } }]` will cause a

‘bind: [Artwork]` is shorthand for `bind: { klass: Artwork }`

‘bind: [Artwork, params]` is shorthand for `bind: { klass: Artwork, object: { id: params } }`

‘bind: [User, { id: current_user.id }] is shorthand for `bind: { klass: User, object: { id: current_user.id } }`

‘bind: [[Artwork], [User, { id: current_user.id }]]` is shorthand for

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

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
  cache_options = cache_options(context)
  CACHE_STRATEGIES.each do |strategy|
    cache_options = strategy.apply(cache_options)
  end
  key = key(binding, key_context(context))
  result = Garner.config.cache.fetch(key, cache_options) 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(meta(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)
  options = index(*args)
  reset_key_prefix_for(options[:klass], options[:object])
  reset_key_prefix_for(options[:klass]) if options[:object]
end