Skip to content

Use block parameter to pipeline in Redis#multi #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/kredis/types/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ class Kredis::Types::Counter < Kredis::Types::Proxying
attr_accessor :expires_in

def increment(by: 1)
multi do
set 0, ex: expires_in, nx: true
incrby by
multi do |pipeline|
pipeline.set 0, ex: expires_in, nx: true
pipeline.incrby by
end[-1]
end

def decrement(by: 1)
multi do
set 0, ex: expires_in, nx: true
decrby by
multi do |pipeline|
pipeline.set 0, ex: expires_in, nx: true
pipeline.decrby by
end[-1]
end

Expand Down
12 changes: 6 additions & 6 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ def elements
end
alias to_a elements

def remove(*elements)
types_to_strings(elements, typed).each { |element| lrem 0, element }
def remove(*elements, pipeline: nil)
types_to_strings(elements, typed).each { |element| (pipeline || proxy).lrem 0, element }
end

def prepend(*elements)
lpush types_to_strings(elements, typed) if elements.flatten.any?
def prepend(*elements, pipeline: nil)
(pipeline || proxy).lpush types_to_strings(elements, typed) if elements.flatten.any?
end

def append(*elements)
rpush types_to_strings(elements, typed) if elements.flatten.any?
def append(*elements, pipeline: nil)
(pipeline || proxy).rpush types_to_strings(elements, typed) if elements.flatten.any?
end
alias << append

Expand Down
9 changes: 7 additions & 2 deletions lib/kredis/types/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ def initialize(redis, key, **options)
options.each { |key, value| send("#{key}=", value) }
end

def multi(...)
redis.multi(...)
def multi(&block)
# NOTE: to be removed when Redis 4 compatibility gets dropped
return redis.multi unless block

redis.multi do |pipeline|
block.call(Kredis::Types::Proxy.new(pipeline, key))
end
end

def method_missing(method, *args, **kwargs)
Expand Down
14 changes: 7 additions & 7 deletions lib/kredis/types/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ def members
end
alias to_a members

def add(*members)
sadd types_to_strings(members, typed) if members.flatten.any?
def add(*members, pipeline: nil)
(pipeline || proxy).sadd types_to_strings(members, typed) if members.flatten.any?
end
alias << add

def remove(*members)
srem types_to_strings(members, typed) if members.flatten.any?
def remove(*members, pipeline: nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency

(pipeline || proxy).srem types_to_strings(members, typed) if members.flatten.any?
end

def replace(*members)
multi do
del
add members
multi do |pipeline|
pipeline.del
add members, pipeline: pipeline
end
end

Expand Down
12 changes: 6 additions & 6 deletions lib/kredis/types/unique_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ def prepend(elements)
elements = Array(elements).uniq
return if elements.empty?

multi do
remove elements
multi do |pipeline|
remove elements, pipeline: pipeline
super
ltrim 0, (limit - 1) if limit
pipeline.ltrim 0, (limit - 1) if limit
end
end

def append(elements)
elements = Array(elements).uniq
return if elements.empty?

multi do
remove elements
multi do |pipeline|
remove elements, pipeline: pipeline
super
ltrim -limit, -1 if limit
pipeline.ltrim -limit, -1 if limit
end
end
alias << append
Expand Down