Skip to content

Commit 88b8c29

Browse files
capripotdhh
andauthored
Use block parameter to pipeline in Redis#multi (#68)
* Use block parameter to pipeline in Redis#multi In preparation of Redis 5, as info message alerts. > Pipelining commands on a Redis instance is deprecated and will be removed in Redis 5.0.0. > > redis.multi do > redis.get("key") > end > > should be replaced by > > redis.multi do |pipeline| > pipeline.get("key") > end > > (called from /app/vendor/bundle/ruby/2.7.0/gems/kredis-1.0.1/lib/kredis/types/proxy.rb:13:in `multi'}> * Pipelined methods need key to be passed along * Proxying multi Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent 7290bd1 commit 88b8c29

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

lib/kredis/types/counter.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ class Kredis::Types::Counter < Kredis::Types::Proxying
44
attr_accessor :expires_in
55

66
def increment(by: 1)
7-
multi do
8-
set 0, ex: expires_in, nx: true
9-
incrby by
7+
multi do |pipeline|
8+
pipeline.set 0, ex: expires_in, nx: true
9+
pipeline.incrby by
1010
end[-1]
1111
end
1212

1313
def decrement(by: 1)
14-
multi do
15-
set 0, ex: expires_in, nx: true
16-
decrby by
14+
multi do |pipeline|
15+
pipeline.set 0, ex: expires_in, nx: true
16+
pipeline.decrby by
1717
end[-1]
1818
end
1919

lib/kredis/types/list.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ def elements
88
end
99
alias to_a elements
1010

11-
def remove(*elements)
12-
types_to_strings(elements, typed).each { |element| lrem 0, element }
11+
def remove(*elements, pipeline: nil)
12+
types_to_strings(elements, typed).each { |element| (pipeline || proxy).lrem 0, element }
1313
end
1414

15-
def prepend(*elements)
16-
lpush types_to_strings(elements, typed) if elements.flatten.any?
15+
def prepend(*elements, pipeline: nil)
16+
(pipeline || proxy).lpush types_to_strings(elements, typed) if elements.flatten.any?
1717
end
1818

19-
def append(*elements)
20-
rpush types_to_strings(elements, typed) if elements.flatten.any?
19+
def append(*elements, pipeline: nil)
20+
(pipeline || proxy).rpush types_to_strings(elements, typed) if elements.flatten.any?
2121
end
2222
alias << append
2323

lib/kredis/types/proxy.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ def initialize(redis, key, **options)
99
options.each { |key, value| send("#{key}=", value) }
1010
end
1111

12-
def multi(...)
13-
redis.multi(...)
12+
def multi(&block)
13+
# NOTE: to be removed when Redis 4 compatibility gets dropped
14+
return redis.multi unless block
15+
16+
redis.multi do |pipeline|
17+
block.call(Kredis::Types::Proxy.new(pipeline, key))
18+
end
1419
end
1520

1621
def method_missing(method, *args, **kwargs)

lib/kredis/types/set.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ def members
88
end
99
alias to_a members
1010

11-
def add(*members)
12-
sadd types_to_strings(members, typed) if members.flatten.any?
11+
def add(*members, pipeline: nil)
12+
(pipeline || proxy).sadd types_to_strings(members, typed) if members.flatten.any?
1313
end
1414
alias << add
1515

16-
def remove(*members)
17-
srem types_to_strings(members, typed) if members.flatten.any?
16+
def remove(*members, pipeline: nil)
17+
(pipeline || proxy).srem types_to_strings(members, typed) if members.flatten.any?
1818
end
1919

2020
def replace(*members)
21-
multi do
22-
del
23-
add members
21+
multi do |pipeline|
22+
pipeline.del
23+
add members, pipeline: pipeline
2424
end
2525
end
2626

lib/kredis/types/unique_list.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ def prepend(elements)
88
elements = Array(elements).uniq
99
return if elements.empty?
1010

11-
multi do
12-
remove elements
11+
multi do |pipeline|
12+
remove elements, pipeline: pipeline
1313
super
14-
ltrim 0, (limit - 1) if limit
14+
pipeline.ltrim 0, (limit - 1) if limit
1515
end
1616
end
1717

1818
def append(elements)
1919
elements = Array(elements).uniq
2020
return if elements.empty?
2121

22-
multi do
23-
remove elements
22+
multi do |pipeline|
23+
remove elements, pipeline: pipeline
2424
super
25-
ltrim -limit, -1 if limit
25+
pipeline.ltrim -limit, -1 if limit
2626
end
2727
end
2828
alias << append

0 commit comments

Comments
 (0)