Method: ActiveSupport::Cache::RedisCacheStore#initialize
- Defined in:
- activesupport/lib/active_support/cache/redis_cache_store.rb
#initialize(error_handler: DEFAULT_ERROR_HANDLER, **redis_options) ⇒ RedisCacheStore
Creates a new Redis cache store.
There are four ways to provide the Redis client used by the cache: the :redis
param can be a Redis instance or a block that returns a Redis instance, or the :url
param can be a string or an array of strings which will be used to create a Redis instance or a Redis::Distributed
instance.
Option Class Result
:redis Proc -> options[:redis].call
:redis Object -> options[:redis]
: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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'activesupport/lib/active_support/cache/redis_cache_store.rb', line 149 def initialize(error_handler: DEFAULT_ERROR_HANDLER, **) = .extract!(*UNIVERSAL_OPTIONS) if = self.class.send(:retrieve_pool_options, ) @redis = ::ConnectionPool.new() { self.class.build_redis(**) } else @redis = self.class.build_redis(**) end @max_key_bytesize = MAX_KEY_BYTESIZE @error_handler = error_handler super() end |