Class: RedisMemo::Options

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

Overview

This class allows users to configure various RedisMemo options. Options can be set in your initializer config/initializers/redis_memo.rb

RedisMemo.configure do |config|
  config.expires_in = 3.hours
  config.global_cache_key_version = SecureRandom.uuid
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(async: nil, compress: nil, compress_threshold: nil, redis: nil, redis_error_handler: nil, tracer: nil, global_cache_key_version: nil, expires_in: nil, max_connection_attempts: nil, disable_all: false, disable_cached_select: false, disabled_models: Set.new) ⇒ Options

Returns a new instance of Options.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redis_memo/options.rb', line 12

def initialize(
  async: nil,
  compress: nil,
  compress_threshold: nil,
  redis: nil,
  redis_error_handler: nil,
  tracer: nil,
  global_cache_key_version: nil,
  expires_in: nil,
  max_connection_attempts: nil,
  disable_all: false,
  disable_cached_select: false,
  disabled_models: Set.new
)
  @async = async
  @compress = compress.nil? ? true : compress
  @compress_threshold = compress_threshold || 1.kilobyte
  @redis_config = redis
  @redis = nil
  @redis_error_handler = redis_error_handler
  @tracer = tracer
  @logger = logger
  @global_cache_key_version = global_cache_key_version
  @expires_in = expires_in
  @max_connection_attempts = ENV['REDIS_MEMO_MAX_ATTEMPTS_PER_REQUEST']&.to_i || max_connection_attempts
  @disable_all = ENV['REDIS_MEMO_DISABLE_ALL'] == 'true' || disable_all
  @disable_cached_select = ENV['REDIS_MEMO_DISABLE_CACHED_SELECT'] == 'true' || disable_cached_select
  @disabled_models = disabled_models
end

Instance Attribute Details

#asyncObject

A handler used to asynchronously perform cache writes and invalidations. If no value is provided, RedisMemo will perform these operations synchronously.



133
134
135
# File 'lib/redis_memo/options.rb', line 133

def async
  @async
end

#cache_out_of_date_handlerObject

Handler called when the cached result does not match the uncached result (sampled at the ‘cache_validation_sample_percentage`). This might indicate that invalidation is happening too slowly or that there are incorrect dependencies specified on a cached method.



143
144
145
# File 'lib/redis_memo/options.rb', line 143

def cache_out_of_date_handler
  @cache_out_of_date_handler
end

#cache_validation_sample_percentageObject

Specify the global sampled percentage of the chance to call the cache validation, a value between 0 to 100, when the value is 100, it will call the handler every time the cached result does not match the uncached result You can also specify inline cache validation sample percentage by memoize_method :method, cache_validation_sample_percentage: #value



138
139
140
# File 'lib/redis_memo/options.rb', line 138

def cache_validation_sample_percentage
  @cache_validation_sample_percentage
end

#compressObject

Passed along to the Rails RedisCacheStore, determines whether or not to compress entries before storing them. default: ‘true`



147
148
149
# File 'lib/redis_memo/options.rb', line 147

def compress
  @compress
end

#compress_thresholdObject

Passed along to the Rails RedisCacheStore, the size threshold for which to compress cached entries. default: 1.kilobyte



151
152
153
# File 'lib/redis_memo/options.rb', line 151

def compress_threshold
  @compress_threshold
end

#connection_poolObject

Configuration values for connecting to RedisMemo using a connection pool. It’s recommended to use a connection pool in multi-threaded applications, or when an async handler is set.



155
156
157
# File 'lib/redis_memo/options.rb', line 155

def connection_pool
  @connection_pool
end

#disable_allObject

A global kill switch to disable all RedisMemo operations.



169
170
171
# File 'lib/redis_memo/options.rb', line 169

def disable_all
  @disable_all
end

#disable_cached_selectObject

A kill switch to disable RedisMemo caching on database queries. This does not disable the invalidation after_save hooks that are installed on memoized models.



173
174
175
# File 'lib/redis_memo/options.rb', line 173

def disable_cached_select
  @disable_cached_select
end

#disabled_modelsObject

A kill switch to set the list of models to disable caching and invalidation after_save hooks on.



176
177
178
# File 'lib/redis_memo/options.rb', line 176

def disabled_models
  @disabled_models
end

#expires_inObject

Passed along to the Rails RedisCacheStore, sets the TTL on cache entries in Redis.



158
159
160
# File 'lib/redis_memo/options.rb', line 158

def expires_in
  @expires_in
end

#global_cache_key_version(&blk) ⇒ Object

Sets the global cache key version. Allows the logger to be dynamically determined at runtime if a blk is given.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/redis_memo/options.rb', line 106

def global_cache_key_version(&blk)
  # this method takes a block to be consistent with the inline memo_method
  # API
  if blk.nil?
    if !@global_cache_key_version.respond_to?(:call)
      return @global_cache_key_version
    end

    @global_cache_key_version&.call
  else
    # save the global cache_key_version eagerly
    @global_cache_key_version = blk
  end
end

#logger(&blk) ⇒ Object

Sets the logger object in RedisMemo. Allows the logger to be dynamically determined at runtime if a blk is given.



94
95
96
97
98
99
100
101
102
# File 'lib/redis_memo/options.rb', line 94

def logger(&blk)
  if blk.nil?
    return @logger if @logger.respond_to?(:warn)

    @logger&.call
  else
    @logger = blk
  end
end

#max_connection_attemptsObject

The max number of failed connection attempts RedisMemo will make for a single request before bypassing the caching layer. This helps make RedisMemo resilient to errors and performance issues when there’s an issue with the Redis cluster itself.



163
164
165
# File 'lib/redis_memo/options.rb', line 163

def max_connection_attempts
  @max_connection_attempts
end

#redis_error_handlerObject

Passed along to the Rails RedisCacheStore, the error handler called for Redis related errors.



166
167
168
# File 'lib/redis_memo/options.rb', line 166

def redis_error_handler
  @redis_error_handler
end

#tracer(&blk) ⇒ Object

Sets the tracer object. Allows the tracer to be dynamically determined at runtime if a blk is given.



82
83
84
85
86
87
88
89
90
# File 'lib/redis_memo/options.rb', line 82

def tracer(&blk)
  if blk.nil?
    return @tracer if @tracer.respond_to?(:trace)

    @tracer&.call
  else
    @tracer = blk
  end
end

Instance Method Details

#disable_model(model) ⇒ Object

Disables the model for caching and invalidation



122
123
124
# File 'lib/redis_memo/options.rb', line 122

def disable_model(model)
  @disabled_models << model
end

#model_disabled_for_caching?(model) ⇒ Boolean

Checks if a model is disabled for redis memo caching

Returns:

  • (Boolean)


127
128
129
# File 'lib/redis_memo/options.rb', line 127

def model_disabled_for_caching?(model)
  ENV["REDIS_MEMO_DISABLE_#{model.table_name.upcase}"] == 'true' || @disabled_models.include?(model)
end

#redisObject

Retrieves the redis client, initializing it if it does not exist yet.



43
44
45
# File 'lib/redis_memo/options.rb', line 43

def redis
  @redis ||= RedisMemo::Redis.new(redis_config)
end

#redis=(config) ⇒ Object

Set configuration values to pass to the Redis client. If multiple configurations are passed to this method, we assume that the first config corresponds to the primary node, and subsequent configurations correspond to replica nodes.

For example, if your urls are specified as <url>,<url>...;<url>,...;..., where ; delimits different clusters and , delimits primary and read replicas, then in your configuration:

RedisMemo.configure do |config|
  config.redis = redis_urls.split(';').map do |urls|
    urls.split(',').map do |url|
      {
        url: url,
        # All timeout values are specified in seconds
        connect_timeout: ENV['REDIS_MEMO_CONNECT_TIMEOUT']&.to_f || 0.2,
        read_timeout: ENV['REDIS_MEMO_READ_TIMEOUT']&.to_f || 0.5,
        write_timeout: ENV['REDIS_MEMO_WRITE_TIMEOUT']&.to_f || 0.5,
        reconnect_attempts: ENV['REDIS_MEMO_RECONNECT_ATTEMPTS']&.to_i || 0
      }
    end
  end
end


74
75
76
77
78
# File 'lib/redis_memo/options.rb', line 74

def redis=(config)
  @redis_config = config
  @redis = nil
  redis
end

#redis_configObject

Retrieves the config values used to initialize the Redis client.



48
49
50
# File 'lib/redis_memo/options.rb', line 48

def redis_config
  @redis_config || {}
end