Module: RedisMemo
- Includes:
- TestOverrides
- Defined in:
- lib/redis_memo/testing.rb,
lib/redis_memo.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 Classes: AfterCommit, ArgumentError, Batch, Cache, ConnectionPool, Future, Memoizable, Middleware, Options, Redis, RuntimeError, Testing, Tracer, WithoutMemoization
Constant Summary collapse
- DefaultOptions =
A process-level
RedisMemo::Optionsinstance that stores the global options. This object can be modified byRedisMemo.configure.memoize_methodallows users to provide method-level configuration. When no callsite-level configuration specified we will use the values inDefaultOptionsas the default value. RedisMemo::Options.new
- THREAD_KEY_WITHOUT_MEMO =
:__redis_memo_without_memo__
Class Method Summary collapse
-
.batch(&blk) ⇒ Object
Batch Redis calls triggered by
memoize_methodto minimize the round trips to Redis. - .checksum(serialized) ⇒ Object
-
.configure {|default_options| ... } ⇒ void
Configure global-level default options.
- .deep_sort_hash(orig_hash) ⇒ Object
- .uuid ⇒ Object
-
.without_memo { ... } ⇒ Object
Configure the wrapped code in the block to skip memoization.
-
.without_memo? ⇒ Boolean
Whether the current execution context has been configured to skip memoization and use the uncached code path.
Methods included from TestOverrides
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.batchblock)
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.
51 52 53 54 55 56 57 |
# File 'lib/redis_memo.rb', line 51 def self.batch(&blk) RedisMemo::Batch.open blk.call RedisMemo::Batch.execute ensure RedisMemo::Batch.close end |
.checksum(serialized) ⇒ Object
60 61 62 |
# File 'lib/redis_memo.rb', line 60 def self.checksum(serialized) Digest::SHA1.base64digest(serialized) 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
30 31 32 |
# File 'lib/redis_memo.rb', line 30 def self.configure(&blk) blk.call(DefaultOptions) end |
.deep_sort_hash(orig_hash) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/redis_memo.rb', line 70 def self.deep_sort_hash(orig_hash) {}.tap do |new_hash| orig_hash.sort.each do |k, v| new_hash[k] = v.is_a?(Hash) ? deep_sort_hash(v) : v end end end |
.uuid ⇒ Object
65 66 67 |
# File 'lib/redis_memo.rb', line 65 def self.uuid SecureRandom.uuid end |
.without_memo { ... } ⇒ Object
Configure the wrapped code in the block to skip memoization.
89 90 91 92 93 94 95 |
# File 'lib/redis_memo.rb', line 89 def self.without_memo prev_value = Thread.current[THREAD_KEY_WITHOUT_MEMO] Thread.current[THREAD_KEY_WITHOUT_MEMO] = true yield ensure Thread.current[THREAD_KEY_WITHOUT_MEMO] = prev_value end |
.without_memo? ⇒ Boolean
Whether the current execution context has been configured to skip memoization and use the uncached code path.
82 83 84 |
# File 'lib/redis_memo.rb', line 82 def self.without_memo? Thread.current[THREAD_KEY_WITHOUT_MEMO] == true end |