Skip to content

Commit 3b59b18

Browse files
authored
Merge pull request #78 from lewispb/lb/resolve-pipelining-deprecation-warnings
Avoid having to pass a Redis pipeline argument around
2 parents 476069f + 516c14a commit 3b59b18

File tree

7 files changed

+43
-36
lines changed

7 files changed

+43
-36
lines changed

lib/kredis.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "active_support"
22
require "active_support/core_ext/module/attribute_accessors"
3+
require "active_support/core_ext/module/attribute_accessors_per_thread"
34

45
require "kredis/version"
56

lib/kredis/types/counter.rb

Lines changed: 6 additions & 6 deletions
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 |pipeline|
8-
pipeline.set 0, ex: expires_in, nx: true
9-
pipeline.incrby by
7+
multi do
8+
set 0, ex: expires_in, nx: true
9+
incrby by
1010
end[-1]
1111
end
1212

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

lib/kredis/types/list.rb

Lines changed: 6 additions & 6 deletions
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, pipeline: nil)
12-
types_to_strings(elements, typed).each { |element| (pipeline || proxy).lrem 0, element }
11+
def remove(*elements)
12+
types_to_strings(elements, typed).each { |element| lrem 0, element }
1313
end
1414

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

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

lib/kredis/types/proxy.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ class Kredis::Types::Proxy
22
require_relative "proxy/failsafe"
33
include Failsafe
44

5-
attr_accessor :redis, :key
5+
attr_accessor :key
6+
7+
thread_mattr_accessor :pipeline
68

79
def initialize(redis, key, **options)
810
@redis, @key = redis, key
911
options.each { |key, value| send("#{key}=", value) }
1012
end
1113

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))
14+
def multi(*args, **kwargs, &block)
15+
redis.multi(*args, **kwargs) do |pipeline|
16+
self.pipeline = pipeline
17+
block.call
18+
ensure
19+
self.pipeline = nil
1820
end
1921
end
2022

@@ -27,6 +29,10 @@ def method_missing(method, *args, **kwargs)
2729
end
2830

2931
private
32+
def redis
33+
pipeline || @redis
34+
end
35+
3036
def log_message(method, *args, **kwargs)
3137
args = args.flatten.reject(&:blank?).presence
3238
kwargs = kwargs.reject { |_k, v| v.blank? }.presence

lib/kredis/types/proxying.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
require "active_support/core_ext/module/delegation"
22

33
class Kredis::Types::Proxying
4-
attr_accessor :proxy, :redis, :key
4+
attr_accessor :proxy, :key
55

66
def self.proxying(*commands)
77
delegate *commands, to: :proxy
88
end
99

1010
def initialize(redis, key, **options)
11-
@redis, @key = redis, key
11+
@key = key
1212
@proxy = Kredis::Types::Proxy.new(redis, key)
1313
options.each { |key, value| send("#{key}=", value) }
1414
end

lib/kredis/types/set.rb

Lines changed: 7 additions & 7 deletions
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, pipeline: nil)
12-
(pipeline || proxy).sadd types_to_strings(members, typed) if members.flatten.any?
11+
def add(*members)
12+
sadd types_to_strings(members, typed) if members.flatten.any?
1313
end
1414
alias << add
1515

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

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

lib/kredis/types/unique_list.rb

Lines changed: 8 additions & 8 deletions
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 |pipeline|
12-
remove elements, pipeline: pipeline
13-
super(elements, pipeline: pipeline)
14-
pipeline.ltrim 0, (limit - 1) if limit
11+
multi do
12+
remove elements
13+
super
14+
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 |pipeline|
23-
remove elements, pipeline: pipeline
24-
super(elements, pipeline: pipeline)
25-
pipeline.ltrim -limit, -1 if limit
22+
multi do
23+
remove elements
24+
super
25+
ltrim -limit, -1 if limit
2626
end
2727
end
2828
alias << append

0 commit comments

Comments
 (0)