Module: RedisMemo

Includes:
TestOverrides
Defined in:
lib/redis_memo/testing.rb,
lib/redis_memo.rb,
lib/redis_memo/errors.rb

Overview

Redis memo can be flaky due to transient network errors (e.g. Redis connection errors), or when used with async handlers. This class allows users to override the default redis-memo behavior to be more robust when testing their code that uses redis-memo.

Defined Under Namespace

Modules: MemoizeMethod, MemoizeQuery, TestOverrides, ThreadLocalVar, Util Classes: AfterCommit, ArgumentError, Batch, Cache, ConnectionPool, Future, Memoizable, Middleware, Options, Railtie, Redis, RuntimeError, Testing, Tracer, WithoutMemoization

Constant Summary collapse

DefaultOptions =

A process-level RedisMemo::Options instance that stores the global options. This object can be modified by RedisMemo.configure.

memoize_method allows users to provide method-level configuration. When no callsite-level configuration specified we will use the values in DefaultOptions as the default value.

RedisMemo::Options.new

Class Method Summary collapse

Methods included from TestOverrides

#without_memoization?

Class Method Details

.batch(&blk) ⇒ Object

Batch Redis calls triggered by memoize_method to minimize the round trips to Redis.

  • Batches cannot be nested

  • When a batch is still open (while still in the RedisMemo.batch block) the return value of any memoized method is a RedisMemo::Future instead of the actual method value

  • The actual method values are returned as a list, in the same order as invoking, after exiting the block

See RedisMemo::Batch for more information.

Examples:

results = RedisMemo.batch do
  5.times { |i| memoized_calculation(i) }
  nil # Not the return value of the block
end
results.size == 5 # true


57
58
59
60
61
62
63
# File 'lib/redis_memo.rb', line 57

def self.batch(&blk)
  RedisMemo::Batch.open
  blk.call
  RedisMemo::Batch.execute
ensure
  RedisMemo::Batch.close
end

.configure {|default_options| ... } ⇒ void

This method returns an undefined value.

Configure global-level default options. Those options will be used unless some options specified at memoize_method callsite level. See RedisMemo::Options for all the possible options.

RedisMemo::DefaultOptions

Yield Parameters:



36
37
38
# File 'lib/redis_memo.rb', line 36

def self.configure(&blk)
  blk.call(DefaultOptions)
end

.with_max_connection_attempts(max_attempts) { ... } ⇒ Object

Set the max connection attempts to Redis per code block. If we fail to connect to Redis more than ‘max_attempts` times, the rest of the code block will fall back to the uncached flow, `RedisMemo.without_memoization`.

Parameters:

  • The (Integer)

    max number of connection attempts.

Yields:

  • no_args the block of code to set the max attempts for.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/redis_memo.rb', line 90

def self.with_max_connection_attempts(max_attempts)
  prev_value = ThreadLocalVar.without_memoization
  ThreadLocalVar.connection_attempts_count = 0
  ThreadLocalVar.max_connection_attempts = max_attempts

  yield
ensure
  ThreadLocalVar.without_memoization = prev_value
  ThreadLocalVar.connection_attempts_count = nil
  ThreadLocalVar.max_connection_attempts = nil
end

.without_memoization { ... } ⇒ Object

Configure the wrapped code in the block to skip memoization.

Yields:

  • no_args The block of code to skip memoization.



76
77
78
79
80
81
82
# File 'lib/redis_memo.rb', line 76

def self.without_memoization
  prev_value = ThreadLocalVar.without_memoization
  ThreadLocalVar.without_memoization = true
  yield
ensure
  ThreadLocalVar.without_memoization = prev_value
end

.without_memoization?Boolean

Whether the current execution context has been configured to skip memoization and use the uncached code path.

Returns:

  • (Boolean)


69
70
71
# File 'lib/redis_memo.rb', line 69

def self.without_memoization?
  RedisMemo::DefaultOptions.disable_all || ThreadLocalVar.without_memoization == true
end