Skip to content

Commit 9bceca7

Browse files
authored
Merge pull request rails#53868 from joshuay03/consider-connection-pool-wrap-in-redis-cache-store
Also handle `ConnectionPool::Wrapper` objects in `RedisCacheStore`
2 parents 5dc0fd8 + 507c06c commit 9bceca7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

activesupport/lib/active_support/cache/redis_cache_store.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ def build_redis_client(**redis_options)
158158
# cache.exist?('bar') # => false
159159
def initialize(error_handler: DEFAULT_ERROR_HANDLER, **redis_options)
160160
universal_options = redis_options.extract!(*UNIVERSAL_OPTIONS)
161+
redis = redis_options[:redis]
162+
163+
already_pool = redis.instance_of?(::ConnectionPool) ||
164+
(redis.respond_to?(:wrapped_pool) && redis.wrapped_pool.instance_of?(::ConnectionPool))
161165

162-
already_pool = redis_options[:redis].instance_of?(::ConnectionPool)
163166
if !already_pool && pool_options = self.class.send(:retrieve_pool_options, redis_options)
164167
@redis = ::ConnectionPool.new(pool_options) { self.class.build_redis(**redis_options) }
165168
else

activesupport/test/cache/stores/redis_cache_store_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_connection_pooling_by_default
328328
assert_equal 5, pool.instance_variable_get(:@timeout)
329329
end
330330

331-
def test_no_connection_pooling_by_default_when_already_pool
331+
def test_no_connection_pooling_by_default_when_already_a_pool
332332
redis = ::ConnectionPool.new(size: 10, timeout: 2.5) { Redis.new }
333333
cache = ActiveSupport::Cache.lookup_store(:redis_cache_store, redis: redis)
334334
pool = cache.redis
@@ -338,6 +338,18 @@ def test_no_connection_pooling_by_default_when_already_pool
338338
assert_equal 2.5, pool.instance_variable_get(:@timeout)
339339
end
340340

341+
def test_no_connection_pooling_by_default_when_already_wrapped_in_a_pool
342+
redis = ::ConnectionPool::Wrapper.new(size: 10, timeout: 2.5) { Redis.new }
343+
cache = ActiveSupport::Cache.lookup_store(:redis_cache_store, redis: redis)
344+
wrapped_redis = cache.redis
345+
assert_kind_of ::Redis, wrapped_redis
346+
assert_same redis, wrapped_redis
347+
pool = wrapped_redis.wrapped_pool
348+
assert_kind_of ::ConnectionPool, pool
349+
assert_equal 10, pool.size
350+
assert_equal 2.5, pool.instance_variable_get(:@timeout)
351+
end
352+
341353
private
342354
def store
343355
[:redis_cache_store]

0 commit comments

Comments
 (0)