Class: RedisMemo::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_memo/future.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, method_id, method_args, dependent_memos, cache_options, method_name_without_memo) ⇒ Future

Returns a new instance of Future.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redis_memo/future.rb', line 8

def initialize(
  ref,
  method_id,
  method_args,
  dependent_memos,
  cache_options,
  method_name_without_memo
)
  @ref = ref
  @method_id = method_id
  @method_args = method_args
  @dependent_memos = dependent_memos
  @cache_options = cache_options
  @method_name_without_memo = method_name_without_memo
  @method_cache_key = nil
  @cache_hit = false
  @cached_result = nil
  @computed_cached_result = false
  @fresh_result = nil
  @computed_fresh_result = false
end

Instance Attribute Details

#method_cache_keyObject



34
35
36
37
# File 'lib/redis_memo/future.rb', line 34

def method_cache_key
  @method_cache_key ||=
    RedisMemo::MemoizeMethod.method_cache_keys([context])&.first || ''
end

Instance Method Details

#contextObject



30
31
32
# File 'lib/redis_memo/future.rb', line 30

def context
  [@method_id, @method_args, @dependent_memos]
end

#execute(cached_results = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/redis_memo/future.rb', line 39

def execute(cached_results=nil)
  if RedisMemo::Batch.current
    raise RedisMemo::RuntimeError, 'Cannot execute future when a batch is still open'
  end

  if cache_hit?(cached_results)
    validate_cache_result =
      RedisMemo::DefaultOptions.cache_validation_sampler&.call(@method_id)

    if validate_cache_result && cached_result != fresh_result
      RedisMemo::DefaultOptions.cache_out_of_date_handler&.call(
        @ref,
        @method_id,
        @method_args,
        cached_result,
        fresh_result,
      )
      return fresh_result
    end

    return cached_result
  end

  fresh_result
end

#resultObject



65
66
67
68
69
70
71
# File 'lib/redis_memo/future.rb', line 65

def result
  unless @computed_cached_result
    raise RedisMemo::RuntimeError, 'Future has not been executed'
  end

  @fresh_result || @cached_result
end