Class: RedisMemo::Memoizable
- Inherits:
-
Object
- Object
- RedisMemo::Memoizable
- Defined in:
- lib/redis_memo/memoizable.rb
Overview
A RedisMemo::Memoizable
is a unit which represent a dependency on a memoized method. Dependencies can be declared recursively as a DAG (directed acyclic graph), meaning that a RedisMemo::Memoizable
object can have other RedisMemo::Memoizable
dependencies.
RedisMemo will recursively extract all the dependencies in the DAG when computing a memoized method’s versioned cache key.
Defined Under Namespace
Modules: Invalidation Classes: Dependency
Instance Attribute Summary collapse
-
#depends_on ⇒ Proc
readonly
A proc representing other memoizables that this object depends on.
-
#props ⇒ Hash
Prop values on the current memoizable.
Class Method Summary collapse
-
.invalidate(instances) ⇒ Object
Invalidates the list of
RedisMemo::Memoizable
objects by bumping the version stored in Redis.
Instance Method Summary collapse
-
#cache_key ⇒ Object
Computes the checksum of the memoizable’s prop values, and returns it as the cache key.
-
#extra_props(**args) ⇒ Object
Add extra props on the current RedisMemo::Memoizable+ object.
-
#initialize(**props) { ... } ⇒ Memoizable
constructor
Creates a new
RedisMemo::Memoizable
object.
Constructor Details
#initialize(**props) { ... } ⇒ Memoizable
Creates a new RedisMemo::Memoizable
object.
36 37 38 39 40 |
# File 'lib/redis_memo/memoizable.rb', line 36 def initialize(**props, &depends_on) @props = props @depends_on = depends_on @cache_key = nil end |
Instance Attribute Details
#depends_on ⇒ Proc (readonly)
Returns A proc representing other memoizables that this object depends on.
29 30 31 |
# File 'lib/redis_memo/memoizable.rb', line 29 def depends_on @depends_on end |
#props ⇒ Hash
Returns prop values on the current memoizable.
26 27 28 |
# File 'lib/redis_memo/memoizable.rb', line 26 def props @props end |
Class Method Details
.invalidate(instances) ⇒ Object
Invalidates the list of RedisMemo::Memoizable
objects by bumping the version stored in Redis.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/redis_memo/memoizable.rb', line 65 def self.invalidate(instances) instances.each do |instance| cache_key = instance.cache_key RedisMemo::Memoizable::Invalidation.__send__( :bump_version_later, cache_key, RedisMemo::Util.uuid, ) end RedisMemo::Memoizable::Invalidation.drain_invalidation_queue end |
Instance Method Details
#cache_key ⇒ Object
Computes the checksum of the memoizable’s prop values, and returns it as the cache key.
52 53 54 55 56 57 58 59 |
# File 'lib/redis_memo/memoizable.rb', line 52 def cache_key @cache_key ||= [ self.class.name, RedisMemo::Util.checksum( RedisMemo::Util.deep_sort_hash(@props).to_json, ), ].join(':') end |
#extra_props(**args) ⇒ Object
Add extra props on the current RedisMemo::Memoizable+ object.
45 46 47 48 49 |
# File 'lib/redis_memo/memoizable.rb', line 45 def extra_props(**args) instance = dup instance.props = props.dup.merge(**args) instance end |