new(error_handler: DEFAULT_ERROR_HANDLER, **redis_options)
public

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 +ActiveSupport::Cache::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 ActiveSupport::Cache::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