Skip to content

Commit 148d007

Browse files
Revert "Merge pull request rails#31447 from fatkodima/redis_cache-connection_pool"
This reverts commit ac74e2c, reversing changes made to ffdb061.
1 parent 8a05dff commit 148d007

File tree

5 files changed

+24
-104
lines changed

5 files changed

+24
-104
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
gem "dalli", ">= 2.2.1"
5353
gem "listen", ">= 3.0.5", "< 3.2", require: false
5454
gem "libxml-ruby", platforms: :ruby
55-
gem "connection_pool", require: false
55+
gem "connection_pool"
5656

5757
# for railties app_generator_test
5858
gem "bootsnap", ">= 1.1.0", require: false

activesupport/lib/active_support/cache.rb

-17
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,6 @@ class Store
160160
attr_reader :silence, :options
161161
alias :silence? :silence
162162

163-
class << self
164-
private
165-
def retrieve_pool_options(options)
166-
{}.tap do |pool_options|
167-
pool_options[:size] = options[:pool_size] if options[:pool_size]
168-
pool_options[:timeout] = options[:pool_timeout] if options[:pool_timeout]
169-
end
170-
end
171-
172-
def ensure_connection_pool_added!
173-
require "connection_pool"
174-
rescue LoadError => e
175-
$stderr.puts "You don't have connection_pool installed in your application. Please add it to your Gemfile and run bundle install"
176-
raise e
177-
end
178-
end
179-
180163
# Creates a new cache. The options will be passed to any write method calls
181164
# except for <tt>:namespace</tt> which can be used to set the global
182165
# namespace for the cache.

activesupport/lib/active_support/cache/mem_cache_store.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,21 @@ def self.build_mem_cache(*addresses) # :nodoc:
6363
addresses = addresses.flatten
6464
options = addresses.extract_options!
6565
addresses = ["localhost:11211"] if addresses.empty?
66-
pool_options = retrieve_pool_options(options)
66+
67+
pool_options = {}
68+
pool_options[:size] = options[:pool_size] if options[:pool_size]
69+
pool_options[:timeout] = options[:pool_timeout] if options[:pool_timeout]
6770

6871
if pool_options.empty?
6972
Dalli::Client.new(addresses, options)
7073
else
71-
ensure_connection_pool_added!
74+
begin
75+
require "connection_pool"
76+
rescue LoadError => e
77+
$stderr.puts "You don't have connection_pool installed in your application. Please add it to your Gemfile and run bundle install"
78+
raise e
79+
end
80+
7281
ConnectionPool.new(pool_options) { Dalli::Client.new(addresses, options.merge(threadsafe: false)) }
7382
end
7483
end

activesupport/lib/active_support/cache/redis_cache_store.rb

+12-52
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,6 @@
2020

2121
module ActiveSupport
2222
module Cache
23-
module ConnectionPoolLike
24-
def with
25-
yield self
26-
end
27-
end
28-
29-
::Redis.include(ConnectionPoolLike)
30-
31-
class RedisDistributedWithConnectionPool < ::Redis::Distributed
32-
def add_node(options)
33-
pool_options = {}
34-
pool_options[:size] = options[:pool_size] if options[:pool_size]
35-
pool_options[:timeout] = options[:pool_timeout] if options[:pool_timeout]
36-
37-
if pool_options.empty?
38-
super
39-
else
40-
options = { url: options } if options.is_a?(String)
41-
options = @default_options.merge(options)
42-
pool = ConnectionPool.new(pool_options) { ::Redis.new(options) }
43-
@ring.add_node(pool)
44-
end
45-
end
46-
end
47-
4823
# Redis cache store.
4924
#
5025
# Deployment note: Take care to use a *dedicated Redis cache* rather
@@ -147,7 +122,7 @@ def build_redis(redis: nil, url: nil, **redis_options) #:nodoc:
147122

148123
private
149124
def build_redis_distributed_client(urls:, **redis_options)
150-
RedisDistributedWithConnectionPool.new([], DEFAULT_REDIS_OPTIONS.merge(redis_options)).tap do |dist|
125+
::Redis::Distributed.new([], DEFAULT_REDIS_OPTIONS.merge(redis_options)).tap do |dist|
151126
urls.each { |u| dist.add_node url: u }
152127
end
153128
end
@@ -197,7 +172,7 @@ def initialize(namespace: nil, compress: true, compress_threshold: 1.kilobyte, e
197172
end
198173

199174
def redis
200-
@redis ||= wrap_in_connection_pool(self.class.build_redis(**redis_options))
175+
@redis ||= self.class.build_redis(**redis_options)
201176
end
202177

203178
def inspect
@@ -236,7 +211,7 @@ def delete_matched(matcher, options = nil)
236211
instrument :delete_matched, matcher do
237212
case matcher
238213
when String
239-
redis.with { |c| c.eval DELETE_GLOB_LUA, [], [namespace_key(matcher, options)] }
214+
redis.eval DELETE_GLOB_LUA, [], [namespace_key(matcher, options)]
240215
else
241216
raise ArgumentError, "Only Redis glob strings are supported: #{matcher.inspect}"
242217
end
@@ -254,7 +229,7 @@ def delete_matched(matcher, options = nil)
254229
def increment(name, amount = 1, options = nil)
255230
instrument :increment, name, amount: amount do
256231
failsafe :increment do
257-
redis.with { |c| c.incrby normalize_key(name, options), amount }
232+
redis.incrby normalize_key(name, options), amount
258233
end
259234
end
260235
end
@@ -270,7 +245,7 @@ def increment(name, amount = 1, options = nil)
270245
def decrement(name, amount = 1, options = nil)
271246
instrument :decrement, name, amount: amount do
272247
failsafe :decrement do
273-
redis.with { |c| c.decrby normalize_key(name, options), amount }
248+
redis.decrby normalize_key(name, options), amount
274249
end
275250
end
276251
end
@@ -292,7 +267,7 @@ def clear(options = nil)
292267
if namespace = merged_options(options)[namespace]
293268
delete_matched "*", namespace: namespace
294269
else
295-
redis.with { |c| c.flushdb }
270+
redis.flushdb
296271
end
297272
end
298273
end
@@ -308,21 +283,6 @@ def mset_capable? #:nodoc:
308283
end
309284

310285
private
311-
def wrap_in_connection_pool(redis_connection)
312-
if redis_connection.is_a?(::Redis)
313-
pool_options = self.class.send(:retrieve_pool_options, redis_options)
314-
315-
if pool_options.empty?
316-
redis_connection
317-
else
318-
self.class.send(:ensure_connection_pool_added!)
319-
ConnectionPool.new(pool_options) { redis_connection }
320-
end
321-
else
322-
redis_connection
323-
end
324-
end
325-
326286
def set_redis_capabilities
327287
case redis
328288
when Redis::Distributed
@@ -338,7 +298,7 @@ def set_redis_capabilities
338298
# Read an entry from the cache.
339299
def read_entry(key, options = nil)
340300
failsafe :read_entry do
341-
deserialize_entry redis.with { |c| c.get(key) }
301+
deserialize_entry redis.get(key)
342302
end
343303
end
344304

@@ -349,7 +309,7 @@ def read_multi_mget(*names)
349309
keys = names.map { |name| normalize_key(name, options) }
350310

351311
values = failsafe(:read_multi_mget, returning: {}) do
352-
redis.with { |c| c.mget(*keys) }
312+
redis.mget(*keys)
353313
end
354314

355315
names.zip(values).each_with_object({}) do |(name, value), results|
@@ -381,17 +341,17 @@ def write_entry(key, entry, unless_exist: false, raw: false, expires_in: nil, ra
381341
modifiers[:nx] = unless_exist
382342
modifiers[:px] = (1000 * expires_in.to_f).ceil if expires_in
383343

384-
redis.with { |c| c.set key, value, modifiers }
344+
redis.set key, value, modifiers
385345
else
386-
redis.with { |c| c.set key, value }
346+
redis.set key, value
387347
end
388348
end
389349
end
390350

391351
# Delete an entry from the cache.
392352
def delete_entry(key, options)
393353
failsafe :delete_entry, returning: false do
394-
redis.with { |c| c.del key }
354+
redis.del key
395355
end
396356
end
397357

@@ -400,7 +360,7 @@ def write_multi_entries(entries, expires_in: nil, **options)
400360
if entries.any?
401361
if mset_capable? && expires_in.nil?
402362
failsafe :write_multi_entries do
403-
redis.with { |c| c.mapped_mset(entries) }
363+
redis.mapped_mset(entries)
404364
end
405365
else
406366
super

activesupport/test/cache/stores/redis_cache_store_test.rb

-32
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@
88
module ActiveSupport::Cache::RedisCacheStoreTests
99
DRIVER = %w[ ruby hiredis ].include?(ENV["REDIS_DRIVER"]) ? ENV["REDIS_DRIVER"] : "hiredis"
1010

11-
# Emulates a latency on Redis's back-end for the key latency to facilitate
12-
# connection pool testing.
13-
class SlowRedis < Redis
14-
def get(key, options = {})
15-
if key =~ /latency/
16-
sleep 3
17-
else
18-
super
19-
end
20-
end
21-
end
22-
2311
class LookupTest < ActiveSupport::TestCase
2412
test "may be looked up as :redis_cache_store" do
2513
assert_kind_of ActiveSupport::Cache::RedisCacheStore,
@@ -122,26 +110,6 @@ class RedisCacheStoreCommonBehaviorTest < StoreTest
122110
include AutoloadingCacheBehavior
123111
end
124112

125-
class RedisCacheStoreConnectionPoolBehaviourTest < StoreTest
126-
include ConnectionPoolBehavior
127-
128-
private
129-
130-
def store
131-
:redis_cache_store
132-
end
133-
134-
def emulating_latency
135-
old_redis = Object.send(:remove_const, :Redis)
136-
Object.const_set(:Redis, SlowRedis)
137-
138-
yield
139-
ensure
140-
Object.send(:remove_const, :Redis)
141-
Object.const_set(:Redis, old_redis)
142-
end
143-
end
144-
145113
# Separate test class so we can omit the namespace which causes expected,
146114
# appropriate complaints about incompatible string encodings.
147115
class KeyEncodingSafetyTest < StoreTest

0 commit comments

Comments
 (0)