20
20
21
21
module ActiveSupport
22
22
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
-
48
23
# Redis cache store.
49
24
#
50
25
# 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:
147
122
148
123
private
149
124
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 |
151
126
urls . each { |u | dist . add_node url : u }
152
127
end
153
128
end
@@ -197,7 +172,7 @@ def initialize(namespace: nil, compress: true, compress_threshold: 1.kilobyte, e
197
172
end
198
173
199
174
def redis
200
- @redis ||= wrap_in_connection_pool ( self . class . build_redis ( **redis_options ) )
175
+ @redis ||= self . class . build_redis ( **redis_options )
201
176
end
202
177
203
178
def inspect
@@ -236,7 +211,7 @@ def delete_matched(matcher, options = nil)
236
211
instrument :delete_matched , matcher do
237
212
case matcher
238
213
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 ) ]
240
215
else
241
216
raise ArgumentError , "Only Redis glob strings are supported: #{ matcher . inspect } "
242
217
end
@@ -254,7 +229,7 @@ def delete_matched(matcher, options = nil)
254
229
def increment ( name , amount = 1 , options = nil )
255
230
instrument :increment , name , amount : amount do
256
231
failsafe :increment do
257
- redis . with { | c | c . incrby normalize_key ( name , options ) , amount }
232
+ redis . incrby normalize_key ( name , options ) , amount
258
233
end
259
234
end
260
235
end
@@ -270,7 +245,7 @@ def increment(name, amount = 1, options = nil)
270
245
def decrement ( name , amount = 1 , options = nil )
271
246
instrument :decrement , name , amount : amount do
272
247
failsafe :decrement do
273
- redis . with { | c | c . decrby normalize_key ( name , options ) , amount }
248
+ redis . decrby normalize_key ( name , options ) , amount
274
249
end
275
250
end
276
251
end
@@ -292,7 +267,7 @@ def clear(options = nil)
292
267
if namespace = merged_options ( options ) [ namespace ]
293
268
delete_matched "*" , namespace : namespace
294
269
else
295
- redis . with { | c | c . flushdb }
270
+ redis . flushdb
296
271
end
297
272
end
298
273
end
@@ -308,21 +283,6 @@ def mset_capable? #:nodoc:
308
283
end
309
284
310
285
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
-
326
286
def set_redis_capabilities
327
287
case redis
328
288
when Redis ::Distributed
@@ -338,7 +298,7 @@ def set_redis_capabilities
338
298
# Read an entry from the cache.
339
299
def read_entry ( key , options = nil )
340
300
failsafe :read_entry do
341
- deserialize_entry redis . with { | c | c . get ( key ) }
301
+ deserialize_entry redis . get ( key )
342
302
end
343
303
end
344
304
@@ -349,7 +309,7 @@ def read_multi_mget(*names)
349
309
keys = names . map { |name | normalize_key ( name , options ) }
350
310
351
311
values = failsafe ( :read_multi_mget , returning : { } ) do
352
- redis . with { | c | c . mget ( *keys ) }
312
+ redis . mget ( *keys )
353
313
end
354
314
355
315
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
381
341
modifiers [ :nx ] = unless_exist
382
342
modifiers [ :px ] = ( 1000 * expires_in . to_f ) . ceil if expires_in
383
343
384
- redis . with { | c | c . set key , value , modifiers }
344
+ redis . set key , value , modifiers
385
345
else
386
- redis . with { | c | c . set key , value }
346
+ redis . set key , value
387
347
end
388
348
end
389
349
end
390
350
391
351
# Delete an entry from the cache.
392
352
def delete_entry ( key , options )
393
353
failsafe :delete_entry , returning : false do
394
- redis . with { | c | c . del key }
354
+ redis . del key
395
355
end
396
356
end
397
357
@@ -400,7 +360,7 @@ def write_multi_entries(entries, expires_in: nil, **options)
400
360
if entries . any?
401
361
if mset_capable? && expires_in . nil?
402
362
failsafe :write_multi_entries do
403
- redis . with { | c | c . mapped_mset ( entries ) }
363
+ redis . mapped_mset ( entries )
404
364
end
405
365
else
406
366
super
0 commit comments