class method
self.new
Ruby on Rails 8.1.2
Since v5.2.8.1Signature
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:
-
The
:redisparam can be:-
A Redis instance.
-
A
ConnectionPoolinstance wrapping a Redis instance. -
A block that returns a Redis instance.
-
-
The
:urlparam can be:-
A string used to create a Redis instance.
-
An array of strings used to create a
Redis::Distributedinstance.
-
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_handlerkey = DEFAULT_ERROR_HANDLER -
redis_optionskeyrest
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