class method self.new

Ruby on Rails edge

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

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

Creates a new Redis cache store.

The :url param can be:

 - A string used to create a RedisClient::Pooled instance.
 - An array of strings used to create a +RedisClient::HashRing+ instance.

Option  Class       Result
:url    String  ->  RedisClient.config(url: ).new_pool
:url    Array   ->  RedisClient::HashRing.new([RedisClient.config(url: ).new_pool, ...])

If you need some advanced configuration for the client, or want to use an alternative implementation like redis-cluster-client, you can pass an already configured client via the :client option:

config.cache_store = :redis_cache_store, client: RedisClient.config(...)
config.cache_store = :redis_cache_store, client: [RedisClient.config(...), RedisClient.config(...)]
config.cache_store = :redis_cache_store, client: -> { RedisClient.config(...) }

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 137
      def initialize(error_handler: DEFAULT_ERROR_HANDLER, **redis_options)
        universal_options = redis_options.extract!(*UNIVERSAL_OPTIONS)
        pool_options = self.class.send(:retrieve_pool_options, redis_options)

        if redis_options.key?(:client)
          client = redis_options.delete(:client)
          clients = Array.wrap(client.respond_to?(:call) ? client.call : client)
        else
          urls = Array.wrap(redis_options.delete(:url))
          urls << nil if urls.empty?
          clients = urls.map do |url|
            RedisClient.config(url: url, protocol: 2, **redis_options)
          end
        end

        clients = clients.map do |c|
          if c.respond_to?(:new_pool)
            c.new_pool(**(pool_options || {}))
          else
            c
          end
        end

        @redis = if clients.size > 1
          RedisClient.ring(clients)
        else
          clients.first
        end

        @error_handler = error_handler

        super(universal_options)
      end

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

Defined in ActiveSupport::Cache::RedisCacheStore

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close