123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::Cache::RedisCacheStore

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Store
Instance Chain:
Inherits: ActiveSupport::Cache::Store
Defined in: activesupport/lib/active_support/cache/redis_cache_store.rb

Overview

Redis Cache Store

Deployment note: Take care to use a dedicated Redis cache rather than pointing this at a persistent Redis server (for example, one used as an Active Job queue). Redis won’t cope well with mixed usage patterns and it won’t expire cache entries by default.

Redis cache server setup guide: redis.io/topics/lru-cache

  • Supports vanilla Redis, hiredis, and Redis::Distributed.

  • Supports Memcached-like sharding across Redises with Redis::Distributed.

  • Fault tolerant. If the Redis server is unavailable, no exceptions are raised. Cache fetches are all misses and writes are dropped.

  • Local cache. Hot in-memory primary cache within block/middleware scope.

  • #read_multi and write_multi support for Redis mget/mset. Use Redis::Distributed 4.0.1+ for distributed mget support.

  • #delete_matched support for Redis KEYS globs.

Constant Summary

Store - Inherited

DEFAULT_POOL_OPTIONS, MAX_KEY_SIZE

Class Attribute Summary

Store - Inherited

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Strategy::LocalCache - Included

Store - Inherited

#cleanup

Cleans up the cache by removing expired entries.

#clear

Clears the entire cache.

#decrement

Decrements an integer value in the cache.

#delete

Deletes an entry in the cache.

#delete_matched

Deletes all entries with keys matching the pattern.

#delete_multi

Deletes multiple entries in the cache.

#exist?

Returns true if the cache contains an entry for the given key.

#fetch

Fetches data from the cache, using the given key.

#fetch_multi

Fetches data from the cache, using the given keys.

#increment

Increments an integer value in the cache.

#mute

Silences the logger within a block.

#read

Reads data from the cache, using the given key.

#read_counter

Reads a counter that was set by #increment / #decrement.

#read_multi

Reads multiple values at once from the cache.

#silence,
#silence!

Silences the logger.

#write

Writes the value to the cache with the key.

#write_counter

Writes a counter that can then be modified by #increment / #decrement.

#write_multi

::ActiveSupport::Cache Storage API to write multiple values at once.

#_instrument, #default_serializer,
#delete_entry

Deletes an entry from the cache implementation.

#delete_multi_entries

Deletes multiples entries in the cache implementation.

#deserialize_entry, #expand_and_namespace_key,
#expanded_key

Expands key to be a consistent string value.

#expanded_version, #get_entry_value, #handle_expired_entry, #handle_invalid_expires_in, #instrument, #instrument_multi,
#key_matcher

Adds the namespace defined in the options to a pattern designed to match keys.

#merged_options

Merges the default options with ones specific to a method call.

#namespace_key

Prefix the key with a namespace string:

#normalize_key

Expands, namespaces and truncates the cache key.

#normalize_options

Normalize aliased options to their canonical form.

#normalize_version,
#read_entry

Reads an entry from the cache implementation.

#read_multi_entries

Reads multiple entries from the cache implementation.

#save_block_result_to_cache, #serialize_entry, #truncate_key, #validate_options,
#write_entry

Writes an entry to the cache implementation.

#write_multi_entries

Writes multiple entries to the cache implementation.

#new_entry

Constructor Details

.new(error_handler: DEFAULT_ERROR_HANDLER, **redis_options) ⇒ RedisCacheStore

Creates a new Redis cache store.

There are a few ways to provide the Redis client used by the cache:

  1. The :redis param can be:

    • A Redis instance.

    • A ConnectionPool instance wrapping a Redis instance.

    • A block that returns a Redis instance.

  2. The :url param can be:

    • A string used to create a Redis instance.

    • An array of strings used to create a Redis::Distributed instance.

If the final Redis instance is not already a ConnectionPool, it will be wrapped in one using Store::DEFAULT_POOL_OPTIONS. These options can be overridden with the :pool param, or the pool can be disabled with :pool: false.

Option  Class       Result
:redis  Object  ->  options[:redis]
:redis  Proc    ->  options[:redis].call
:url    String  ->  Redis.new(url: )
:url    Array   ->  Redis::Distributed.new([{ url:  }, { url:  }, ])

No namespace is set by default. Provide one if the Redis cache server is shared with other apps: namespace: 'myapp-cache'.

Compression is enabled by default with a 1kB threshold, so cached values larger than 1kB are automatically compressed. Disable by passing compress: false or change the threshold by passing compress_threshold: 4.kilobytes.

No expiry is set on cache entries by default. Redis is expected to be configured with an eviction policy that automatically deletes least-recently or -frequently used keys when it reaches max memory. See redis.io/topics/lru-cache for cache server setup.

Race condition TTL is not set by default. This can be used to avoid “thundering herd” cache writes when hot cache entries are expired. See Store#fetch for more.

Setting skip_nil: true will not cache nil results:

cache.fetch('foo') { nil }
cache.fetch('bar', skip_nil: true) { nil }
cache.exist?('foo') # => true
cache.exist?('bar') # => false
[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 155

def initialize(error_handler: DEFAULT_ERROR_HANDLER, **redis_options)
  universal_options = redis_options.extract!(*UNIVERSAL_OPTIONS)
  redis = redis_options[:redis]

  already_pool = redis.instance_of?(::ConnectionPool) ||
                 (redis.respond_to?(:wrapped_pool) && redis.wrapped_pool.instance_of?(::ConnectionPool))

  if !already_pool && pool_options = self.class.send(:retrieve_pool_options, redis_options)
    @redis = ::ConnectionPool.new(pool_options) { self.class.build_redis(**redis_options) }
  else
    @redis = self.class.build_redis(**redis_options)
  end

  @error_handler = error_handler

  super(universal_options)
end

Class Attribute Details

.supports_cache_versioning?Boolean (readonly)

Advertise cache versioning support.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 60

def self.supports_cache_versioning?
  true
end

Class Method Details

.build_redis(redis: nil, url: nil, **redis_options)

This method is for internal use only.

Factory method to create a new Redis instance.

Handles four options: :redis block, :redis instance, single :url string, and multiple :url strings.

Option  Class       Result
:redis  Proc    ->  options[:redis].call
:redis  Object  ->  options[:redis]
:url    String  ->  Redis.new(url: )
:url    Array   ->  Redis::Distributed.new([{ url:  }, { url:  }, ])
[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 78

def build_redis(redis: nil, url: nil, **redis_options) # :nodoc:
  urls = Array(url)

  if redis.is_a?(Proc)
    redis.call
  elsif redis
    redis
  elsif urls.size > 1
    build_redis_distributed_client(urls: urls, **redis_options)
  elsif urls.empty?
    build_redis_client(**redis_options)
  else
    build_redis_client(url: urls.first, **redis_options)
  end
end

.build_redis_client(**redis_options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 101

def build_redis_client(**redis_options)
  ::Redis.new(DEFAULT_REDIS_OPTIONS.merge(redis_options))
end

.build_redis_distributed_client(urls:, **redis_options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 95

def build_redis_distributed_client(urls:, **redis_options)
  ::Redis::Distributed.new([], DEFAULT_REDIS_OPTIONS.merge(redis_options)).tap do |dist|
    urls.each { |u| dist.add_node url: u }
  end
end

Instance Attribute Details

#redis (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 106

attr_reader :redis

#supports_expire_nx?Boolean (readonly, private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 483

def supports_expire_nx?
  return @supports_expire_nx if defined?(@supports_expire_nx)

  redis_versions = redis.then { |c| Array.wrap(c.info("server")).pluck("redis_version") }
  @supports_expire_nx = redis_versions.all? { |v| Gem::Version.new(v) >= Gem::Version.new("7.0.0") }
end

Instance Method Details

#change_counter(key, amount, options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 456

def change_counter(key, amount, options)
  redis.then do |c|
    c = c.node_for(key) if c.is_a?(Redis::Distributed)

    expires_in = options[:expires_in]

    if expires_in
      if supports_expire_nx?
        count, _ = c.pipelined do |pipeline|
          pipeline.incrby(key, amount)
          pipeline.call(:expire, key, expires_in.to_i, "NX")
        end
      else
        count, ttl = c.pipelined do |pipeline|
          pipeline.incrby(key, amount)
          pipeline.ttl(key)
        end
        c.expire(key, expires_in.to_i) if ttl < 0
      end
    else
      count = c.incrby(key, amount)
    end

    count
  end
end

#cleanup(options = nil)

::ActiveSupport::Cache Store API implementation.

Removes expired entries. Handled natively by Redis least-recently-/ least-frequently-used expiry, so manual cleanup is not supported.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 301

def cleanup(options = nil)
  super
end

#clear(options = nil)

Clear the entire cache on all Redis servers. Safe to use on shared servers if the cache is namespaced.

Failsafe: Raises errors.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 309

def clear(options = nil)
  failsafe :clear do
    if namespace = merged_options(options)[:namespace]
      delete_matched "*", namespace: namespace
    else
      redis.then { |c| c.flushdb }
    end
  end
end

#decrement(name, amount = 1, options = nil)

Decrement a cached integer value using the Redis decrby atomic operator. Returns the updated value.

If the key is unset or has expired, it will be set to -amount:

cache.decrement("foo") # => -1

To set a specific value, call #write passing raw: true:

cache.write("baz", 5, raw: true)
cache.decrement("baz") # => 4

Decrementing a non-numeric value, or a value written without raw: true, will fail and return nil.

To read the value later, call #read_counter:

cache.decrement("baz") # => 3
cache.read_counter("baz") # 3

Failsafe: Raises errors.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 286

def decrement(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument :decrement, key, amount: amount do
    failsafe :decrement do
      change_counter(key, -amount, options)
    end
  end
end

#delete_entry(key, **options) (private)

Delete an entry from the cache.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 404

def delete_entry(key, **options)
  failsafe :delete_entry, returning: false do
    redis.then { |c| c.unlink(key) == 1 }
  end
end

#delete_matched(matcher, options = nil)

::ActiveSupport::Cache Store API implementation.

Supports Redis KEYS glob patterns:

h?llo matches hello, hallo and hxllo
h*llo matches hllo and heeeello
h[ae]llo matches hello and hallo, but not hillo
h[^e]llo matches hallo, hbllo, ... but not hello
h[a-b]llo matches hallo and hbllo

Use \ to escape special characters if you want to match them verbatim.

See redis.io/commands/KEYS for more.

Failsafe: Raises errors.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 210

def delete_matched(matcher, options = nil)
  unless String === matcher
    raise ArgumentError, "Only Redis glob strings are supported: #{matcher.inspect}"
  end
  pattern = namespace_key(matcher, options)

  instrument :delete_matched, pattern do
    redis.then do |c|
      cursor = "0"
      # Fetch keys in batches using SCAN to avoid blocking the Redis server.
      nodes = c.respond_to?(:nodes) ? c.nodes : [c]

      nodes.each do |node|
        begin
          cursor, keys = node.scan(cursor, match: pattern, count: SCAN_BATCH_SIZE)
          node.unlink(*keys) unless keys.empty?
        end until cursor == "0"
      end
    end
  end
end

#delete_multi_entries(entries, **_options) (private)

Deletes multiple entries in the cache. Returns the number of entries deleted.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 411

def delete_multi_entries(entries, **_options)
  return 0 if entries.empty?

  failsafe :delete_multi_entries, returning: 0 do
    redis.then { |c| c.unlink(*entries) }
  end
end

#deserialize_entry(payload, raw: false) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 434

def deserialize_entry(payload, raw: false, **)
  if raw && !payload.nil?
    Entry.new(payload)
  else
    super(payload)
  end
end

#failsafe(method, returning: nil) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 490

def failsafe(method, returning: nil)
  yield
rescue ::Redis::BaseError, ConnectionPool::Error, ConnectionPool::TimeoutError => error
  @error_handler&.call(method: method, exception: error, returning: returning)
  returning
end

#increment(name, amount = 1, options = nil)

Increment a cached integer value using the Redis incrby atomic operator. Returns the updated value.

If the key is unset or has expired, it will be set to amount:

cache.increment("foo") # => 1
cache.increment("bar", 100) # => 100

To set a specific value, call #write passing raw: true:

cache.write("baz", 5, raw: true)
cache.increment("baz") # => 6

Incrementing a non-numeric value, or a value written without raw: true, will fail and return nil.

To read the value later, call #read_counter:

cache.increment("baz") # => 7
cache.read_counter("baz") # 7

Failsafe: Raises errors.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 254

def increment(name, amount = 1, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  instrument :increment, key, amount: amount do
    failsafe :increment do
      change_counter(key, amount, options)
    end
  end
end

#inspect

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 173

def inspect
  "#<#{self.class} options=#{options.inspect} redis=#{redis.inspect}>"
end

#pipeline_entries(entries, &block) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 325

def pipeline_entries(entries, &block)
  redis.then { |c|
    if c.is_a?(Redis::Distributed)
      entries.group_by { |k, _v| c.node_for(k) }.each do |node, sub_entries|
        node.pipelined { |pipe| yield(pipe, sub_entries) }
      end
    else
      c.pipelined { |pipe| yield(pipe, entries) }
    end
  }
end

#read_entry(key, **options) (private)

Store provider interface: Read an entry from the cache.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 339

def read_entry(key, **options)
  deserialize_entry(read_serialized_entry(key, **options), **options)
end

#read_multi(*names)

::ActiveSupport::Cache Store API implementation.

Read multiple values at once. Returns a hash of requested keys -> fetched values.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 181

def read_multi(*names)
  return {} if names.empty?

  options = names.extract_options!
  options = merged_options(options)
  keys    = names.map { |name| normalize_key(name, options) }

  instrument_multi(:read_multi, keys, options) do |payload|
    read_multi_entries(names, **options).tap do |results|
      payload[:hits] = results.keys.map { |name| normalize_key(name, options) }
    end
  end
end

#read_multi_entries(names, **options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 349

def read_multi_entries(names, **options)
  options = merged_options(options)
  return {} if names == []
  raw = options&.fetch(:raw, false)

  keys = names.map { |name| normalize_key(name, options) }

  values = failsafe(:read_multi_entries, returning: {}) do
    redis.then { |c| c.mget(*keys) }
  end

  names.zip(values).each_with_object({}) do |(name, value), results|
    if value
      entry = deserialize_entry(value, raw: raw)
      unless entry.nil? || entry.expired? || entry.mismatched?(normalize_version(name, options))
        begin
          results[name] = entry.value
        rescue DeserializationError
        end
      end
    end
  end
end

#read_serialized_entry(key, raw: false, **options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 343

def read_serialized_entry(key, raw: false, **options)
  failsafe :read_entry do
    redis.then { |c| c.get(key) }
  end
end

#serialize_entries(entries, **options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 450

def serialize_entries(entries, **options)
  entries.transform_values do |entry|
    serialize_entry(entry, **options)
  end
end

#serialize_entry(entry, raw: false, **options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 442

def serialize_entry(entry, raw: false, **options)
  if raw
    entry.value.to_s
  else
    super(entry, raw: raw, **options)
  end
end

#stats

Get info from redis servers.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 320

def stats
  redis.then { |c| c.info }
end

#write_entry(key, entry, raw: false, **options) (private)

Write an entry to the cache.

Requires Redis 2.6.12+ for extended SET options.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 376

def write_entry(key, entry, raw: false, **options)
  write_serialized_entry(key, serialize_entry(entry, raw: raw, **options), raw: raw, **options)
end

#write_multi_entries(entries, **options) (private)

Nonstandard store provider API to write multiple values at once.

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 420

def write_multi_entries(entries, **options)
  return if entries.empty?

  failsafe :write_multi_entries do
    pipeline_entries(entries) do |pipeline, sharded_entries|
      options = options.dup
      options[:pipeline] = pipeline
      sharded_entries.each do |key, entry|
        write_entry key, entry, **options
      end
    end
  end
end

#write_serialized_entry(key, payload, raw: false, unless_exist: false, expires_in: nil, race_condition_ttl: nil, pipeline: nil, **options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 380

def write_serialized_entry(key, payload, raw: false, unless_exist: false, expires_in: nil, race_condition_ttl: nil, pipeline: nil, **options)
  # If race condition TTL is in use, ensure that cache entries
  # stick around a bit longer after they would have expired
  # so we can purposefully serve stale entries.
  if race_condition_ttl && expires_in && expires_in > 0 && !raw
    expires_in += 5.minutes
  end

  modifiers = {}
  if unless_exist || expires_in
    modifiers[:nx] = unless_exist
    modifiers[:px] = (1000 * expires_in.to_f).ceil if expires_in
  end

  if pipeline
    pipeline.set(key, payload, **modifiers)
  else
    failsafe :write_entry, returning: nil do
      redis.then { |c| !!c.set(key, payload, **modifiers) }
    end
  end
end