Class: RedisMemo::Future
- Inherits:
-
Object
- Object
- RedisMemo::Future
- Defined in:
- lib/redis_memo/future.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #context ⇒ Object
- #execute(cached_results = nil) ⇒ Object
-
#initialize(ref, method_id, method_args, dependent_memos, cache_options, method_name_without_memoization) ⇒ Future
constructor
A new instance of Future.
- #result ⇒ Object
- #validate_cache_result ⇒ Object
Constructor Details
#initialize(ref, method_id, method_args, dependent_memos, cache_options, method_name_without_memoization) ⇒ Future
Returns a new instance of Future.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/redis_memo/future.rb', line 9 def initialize( ref, method_id, method_args, dependent_memos, , method_name_without_memoization ) @ref = ref @method_id = method_id @method_args = method_args @dependent_memos = dependent_memos @cache_options = @method_name_without_memoization = method_name_without_memoization @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_key ⇒ Object
35 36 37 38 |
# File 'lib/redis_memo/future.rb', line 35 def method_cache_key @method_cache_key ||= RedisMemo::MemoizeMethod.__send__(:method_cache_keys, [context])&.first || '' end |
Instance Method Details
#context ⇒ Object
31 32 33 |
# File 'lib/redis_memo/future.rb', line 31 def context [@method_id, @method_args, @dependent_memos] end |
#execute(cached_results = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/redis_memo/future.rb', line 51 def execute(cached_results = nil) if RedisMemo::Batch.current raise RedisMemo::RuntimeError.new('Cannot execute future when a batch is still open') end if cache_hit?(cached_results) 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 |
#result ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/redis_memo/future.rb', line 74 def result unless @computed_cached_result raise RedisMemo::RuntimeError.new('Future has not been executed') end @fresh_result || @cached_result end |
#validate_cache_result ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/redis_memo/future.rb', line 40 def validate_cache_result cache_validation_sample_percentage = @cache_options[:cache_validation_sample_percentage] || RedisMemo::DefaultOptions.cache_validation_sample_percentage if cache_validation_sample_percentage.nil? false else cache_validation_sample_percentage > Random.rand(0...100) end end |