class method self.new

Ruby on Rails 8.1.2

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

self.new(error_handler: DEFAULT_ERROR_HANDLER, **redis_options)

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

Parameters

error_handler key = DEFAULT_ERROR_HANDLER
redis_options keyrest
Source
# 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

Defined in activesupport/lib/active_support/cache/redis_cache_store.rb line 155 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::Cache::RedisCacheStore

Type at least 2 characters to search.

↑↓ navigate · open · esc close