Class: RedisMemo::Memoizable

Inherits:
Object
  • Object
show all
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.

Examples:

memo = RedisMemo::Memoizable.new(a: 1) do
  depends_on RedisMemo::Memoizable.new(b: 3)
  depends_on RedisMemo::Memoizable.new(c: 4)
end

memoize_method :method_name do
  depends_on memo
end

Defined Under Namespace

Modules: Invalidation Classes: Dependency

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**props) { ... } ⇒ Memoizable

Creates a new RedisMemo::Memoizable object.

Parameters:

  • props (Hash)

Yields:

  • depends_on A dependency block representing other RedisMemo::Memoizables that this object depends on.



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_onProc (readonly)

Returns A proc representing other memoizables that this object depends on.

Returns:

  • (Proc)

    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

#propsHash

Returns prop values on the current memoizable.

Returns:

  • (Hash)

    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.

Parameters:



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_keyObject

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