Skip to content

Commit e164798

Browse files
committed
Proxying multi
1 parent b2ee8e4 commit e164798

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

lib/kredis/types/counter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
class Kredis::Types::Counter < Kredis::Types::Proxying
2-
proxying :multi, :set, :get, :del, :exists?
2+
proxying :multi, :set, :incrby, :decrby, :get, :del, :exists?
33

44
attr_accessor :expires_in
55

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

1313
def decrement(by: 1)
1414
multi do |pipeline|
15-
pipeline.set key, 0, ex: expires_in, nx: true
16-
pipeline.decrby key, by
15+
pipeline.set 0, ex: expires_in, nx: true
16+
pipeline.decrby by
1717
end
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)
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
end

lib/kredis/types/proxy.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ 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+
return redis.multi unless block
14+
15+
redis.multi do |pipeline|
16+
block.call(Kredis::Types::Proxy.new(pipeline, key))
17+
end
1418
end
1519

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

lib/kredis/types/set.rb

Lines changed: 6 additions & 6 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)
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)
2121
multi do |pipeline|
22-
pipeline.del key
23-
pipeline.sadd key, types_to_strings(members, typed) if members.flatten.any?
22+
pipeline.del
23+
add members, pipeline: pipeline
2424
end
2525
end
2626

lib/kredis/types/unique_list.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# You'd normally call this a set, but Redis already has another data type for that
22
class Kredis::Types::UniqueList < Kredis::Types::List
3-
proxying :multi, :exists?
3+
proxying :multi, :ltrim, :exists?
44

55
attr_accessor :typed, :limit
66

@@ -9,9 +9,9 @@ def prepend(elements)
99
return if elements.empty?
1010

1111
multi do |pipeline|
12-
types_to_strings(elements, typed).each { |element| pipeline.lrem key, 0, element }
12+
remove elements, pipeline: pipeline
1313
super
14-
pipeline.ltrim key, 0, (limit - 1) if limit
14+
pipeline.ltrim 0, (limit - 1) if limit
1515
end
1616
end
1717

@@ -20,9 +20,9 @@ def append(elements)
2020
return if elements.empty?
2121

2222
multi do |pipeline|
23-
types_to_strings(elements, typed).each { |element| pipeline.lrem key, 0, element }
23+
remove elements, pipeline: pipeline
2424
super
25-
pipeline.ltrim key, -limit, -1 if limit
25+
pipeline.ltrim -limit, -1 if limit
2626
end
2727
end
2828
alias << append

0 commit comments

Comments
 (0)