Skip to content

Commit

Permalink
Merge pull request #78 from lewispb/lb/resolve-pipelining-deprecation…
Browse files Browse the repository at this point in the history
…-warnings

Avoid having to pass a Redis pipeline argument around
  • Loading branch information
jeremy committed Jun 29, 2022
2 parents 476069f + 516c14a commit 3b59b18
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 36 deletions.
1 change: 1 addition & 0 deletions lib/kredis.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "active_support"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/module/attribute_accessors_per_thread"

require "kredis/version"

Expand Down
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 |pipeline|
pipeline.set 0, ex: expires_in, nx: true
pipeline.incrby by
multi do
set 0, ex: expires_in, nx: true
incrby by
end[-1]
end

def decrement(by: 1)
multi do |pipeline|
pipeline.set 0, ex: expires_in, nx: true
pipeline.decrby by
multi do
set 0, ex: expires_in, nx: true
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, pipeline: nil)
types_to_strings(elements, typed).each { |element| (pipeline || proxy).lrem 0, element }
def remove(*elements)
types_to_strings(elements, typed).each { |element| lrem 0, element }
end

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

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

Expand Down
20 changes: 13 additions & 7 deletions lib/kredis/types/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ class Kredis::Types::Proxy
require_relative "proxy/failsafe"
include Failsafe

attr_accessor :redis, :key
attr_accessor :key

thread_mattr_accessor :pipeline

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

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))
def multi(*args, **kwargs, &block)
redis.multi(*args, **kwargs) do |pipeline|
self.pipeline = pipeline
block.call
ensure
self.pipeline = nil
end
end

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

private
def redis
pipeline || @redis
end

def log_message(method, *args, **kwargs)
args = args.flatten.reject(&:blank?).presence
kwargs = kwargs.reject { |_k, v| v.blank? }.presence
Expand Down
4 changes: 2 additions & 2 deletions lib/kredis/types/proxying.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "active_support/core_ext/module/delegation"

class Kredis::Types::Proxying
attr_accessor :proxy, :redis, :key
attr_accessor :proxy, :key

def self.proxying(*commands)
delegate *commands, to: :proxy
end

def initialize(redis, key, **options)
@redis, @key = redis, key
@key = key
@proxy = Kredis::Types::Proxy.new(redis, key)
options.each { |key, value| send("#{key}=", value) }
end
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, pipeline: nil)
(pipeline || proxy).sadd types_to_strings(members, typed) if members.flatten.any?
def add(*members)
sadd types_to_strings(members, typed) if members.flatten.any?
end
alias << add

def remove(*members, pipeline: nil)
(pipeline || proxy).srem types_to_strings(members, typed) if members.flatten.any?
def remove(*members)
srem types_to_strings(members, typed) if members.flatten.any?
end

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

Expand Down
16 changes: 8 additions & 8 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 |pipeline|
remove elements, pipeline: pipeline
super(elements, pipeline: pipeline)
pipeline.ltrim 0, (limit - 1) if limit
multi do
remove elements
super
ltrim 0, (limit - 1) if limit
end
end

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

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

0 comments on commit 3b59b18

Please sign in to comment.