Skip to content

Commit

Permalink
Merge branch 'main' into disable-failover
Browse files Browse the repository at this point in the history
* main:
  Coalesce "current pipeline or redis" into the redis method itself
  Pefer a thread_mattr_accessor over a thread local variable
  Delete list of keys in batch (rails#90)
  Use a thread-local variable for pipeline
  Revert "Use block parameter to pipeline in Redis#multi (rails#68)"
  Resolve pipelining deprecation warnings
  • Loading branch information
lewispb committed Jul 1, 2022
2 parents cc5c773 + 3b59b18 commit f1a895c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 40 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: 9 additions & 3 deletions lib/kredis/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ def migrate(from:, to:, pipeline: nil)
end
end

def delete_all(key_pattern)
each_key_batch_matching(key_pattern) do |keys, pipeline|
pipeline.del *keys
def delete_all(*key_patterns)
log_migration "DELETE ALL #{key_patterns.inspect}" do
if key_patterns.length > 1
@redis.del *key_patterns
else
each_key_batch_matching(key_patterns.first) do |keys, pipeline|
pipeline.del *keys
end
end
end
end

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
10 changes: 9 additions & 1 deletion test/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,19 @@ class MigrationTest < ActiveSupport::TestCase
end
end

test "delete_all" do
test "delete_all with pattern" do
3.times { |index| Kredis.proxy("mykey:#{index}").set "hello there #{index}" }

Kredis::Migration.delete_all "mykey:*"

3.times { |index| assert_nil Kredis.proxy("mykey:#{index}").get }
end

test "delete_all with keys" do
3.times { |index| Kredis.proxy("mykey:#{index}").set "hello there #{index}" }

Kredis::Migration.delete_all *3.times.map { |index| "mykey:#{index}" }

3.times { |index| assert_nil Kredis.proxy("mykey:#{index}").get }
end
end

0 comments on commit f1a895c

Please sign in to comment.