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, max_query_dependency_size: 5000, 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
41
42
# 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,
  max_query_dependency_size: 5000,
  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
  @max_query_dependency_size = ENV['REDIS_MEMO_MAX_QUERY_DEPENDENCY_SIZE']&.to_i || max_query_dependency_size
  @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.



135
136
137
# File 'lib/redis_memo/options.rb', line 135

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.



145
146
147
# File 'lib/redis_memo/options.rb', line 145

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



140
141
142
# File 'lib/redis_memo/options.rb', line 140

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`



149
150
151
# File 'lib/redis_memo/options.rb', line 149

def compress
  @compress
end

#compress_thresholdObject

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



153
154
155
# File 'lib/redis_memo/options.rb', line 153

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.



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

def connection_pool
  @connection_pool
end

#disable_allObject

A global kill switch to disable all RedisMemo operations.



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

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.



178
179
180
# File 'lib/redis_memo/options.rb', line 178

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.



181
182
183
# File 'lib/redis_memo/options.rb', line 181

def disabled_models
  @disabled_models
end

#expires_inObject

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



160
161
162
# File 'lib/redis_memo/options.rb', line 160

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.



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

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.



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

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.



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

def max_connection_attempts
  @max_connection_attempts
end

#max_query_dependency_sizeObject

Only cache a SQL query when the max number of dependency is smaller or equal to this number. Configurable via an ENV var REDIS_MEMO_MAX_QUERY_DEPENDENCY_SIZE. Default at 5000.



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

def max_query_dependency_size
  @max_query_dependency_size
end

#redis_error_handlerObject

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



171
172
173
# File 'lib/redis_memo/options.rb', line 171

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.



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

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



124
125
126
# File 'lib/redis_memo/options.rb', line 124

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)


129
130
131
# File 'lib/redis_memo/options.rb', line 129

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.



45
46
47
# File 'lib/redis_memo/options.rb', line 45

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


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

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

#redis_configObject

Retrieves the config values used to initialize the Redis client.



50
51
52
# File 'lib/redis_memo/options.rb', line 50

def redis_config
  @redis_config || {}
end