Skip to content

Commit f86f5d5

Browse files
committed
Merge branch 'main' into unique_list_with_sorted_set
* main: (21 commits) Bump version for 1.4.0 Update nokogiri for compatibility Revert "Improved version of UniqueList: OrderedSet (rails#76)" (rails#111) Add `last` to lists (rails#97) Improved version of UniqueList: OrderedSet (rails#76) Return Time objects instead of deprecated DateTime (rails#106) Fix possible deserialization of untrusted data Typecast return of Set#take (rails#105) Declare Active Model dependency (rails#107) Address LogSubscriber deprecation (rails#98) Account for time zones in DateTime serializations (rails#102) Add sample to set (rails#100) Bump version for 1.3.0 Allow Redis 5.x Add ltrim to lists 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)" ...
2 parents cfe0310 + a781950 commit f86f5d5

File tree

18 files changed

+130
-62
lines changed

18 files changed

+130
-62
lines changed

Gemfile.lock

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
PATH
22
remote: .
33
specs:
4-
kredis (1.2.0)
4+
kredis (1.4.0)
5+
activemodel (>= 6.0.0)
56
activesupport (>= 6.0.0)
6-
redis (~> 4.2)
7+
redis (>= 4.2, < 6)
78

89
GEM
910
remote: https://rubygems.org/
@@ -70,6 +71,7 @@ GEM
7071
builder (3.2.4)
7172
byebug (11.1.3)
7273
concurrent-ruby (1.1.8)
74+
connection_pool (2.4.1)
7375
crass (1.0.6)
7476
erubi (1.10.0)
7577
globalid (0.4.2)
@@ -84,17 +86,17 @@ GEM
8486
marcel (1.0.1)
8587
method_source (1.0.0)
8688
mini_mime (1.0.3)
87-
mini_portile2 (2.5.0)
89+
mini_portile2 (2.8.2)
8890
minitest (5.14.4)
8991
nio4r (2.5.7)
90-
nokogiri (1.11.2)
91-
mini_portile2 (~> 2.5.0)
92+
nokogiri (1.15.2)
93+
mini_portile2 (~> 2.8.2)
9294
racc (~> 1.4)
93-
nokogiri (1.11.2-arm64-darwin)
95+
nokogiri (1.15.2-arm64-darwin)
9496
racc (~> 1.4)
95-
nokogiri (1.11.2-x86_64-darwin)
97+
nokogiri (1.15.2-x86_64-darwin)
9698
racc (~> 1.4)
97-
racc (1.5.2)
99+
racc (1.7.1)
98100
rack (2.2.3)
99101
rack-test (1.1.0)
100102
rack (>= 1.0, < 3)
@@ -125,7 +127,10 @@ GEM
125127
rake (>= 0.8.7)
126128
thor (~> 1.0)
127129
rake (13.0.3)
128-
redis (4.6.0)
130+
redis (5.0.6)
131+
redis-client (>= 0.9.0)
132+
redis-client (0.14.1)
133+
connection_pool
129134
sprockets (4.0.2)
130135
concurrent-ruby (~> 1.0)
131136
rack (> 1, < 3)
@@ -142,9 +147,9 @@ GEM
142147
zeitwerk (2.4.2)
143148

144149
PLATFORMS
145-
arm64-darwin-20
150+
arm64-darwin
146151
ruby
147-
x86_64-darwin-20
152+
x86_64-darwin
148153

149154
DEPENDENCIES
150155
byebug

kredis.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Gem::Specification.new do |s|
1111

1212
s.required_ruby_version = ">= 2.7.0"
1313
s.add_dependency "activesupport", ">= 6.0.0"
14-
s.add_dependency "redis", "~> 4.2"
14+
s.add_dependency "activemodel", ">= 6.0.0"
15+
s.add_dependency "redis", ">= 4.2", "< 6"
1516
s.add_development_dependency "rails", ">= 6.0.0"
1617

1718
s.files = Dir["lib/**/*", "MIT-LICENSE", "README.md"]

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/log_subscriber.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def meta(event)
1515

1616
private
1717
def formatted_in(color, event, type: nil)
18-
color " Kredis #{type} (#{event.duration.round(1)}ms) #{event.payload[:message]}", color, true
18+
color " Kredis #{type} (#{event.duration.round(1)}ms) #{event.payload[:message]}", color, bold: true
1919
end
2020
end
2121

lib/kredis/migration.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ def migrate(from:, to:, pipeline: nil)
3030
end
3131
end
3232

33-
def delete_all(key_pattern)
34-
each_key_batch_matching(key_pattern) do |keys, pipeline|
35-
pipeline.del *keys
33+
def delete_all(*key_patterns)
34+
log_migration "DELETE ALL #{key_patterns.inspect}" do
35+
if key_patterns.length > 1
36+
@redis.del *key_patterns
37+
else
38+
each_key_batch_matching(key_patterns.first) do |keys, pipeline|
39+
pipeline.del *keys
40+
end
41+
end
3642
end
3743
end
3844

lib/kredis/type/datetime.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module Kredis
44
module Type
55
class DateTime < ActiveModel::Type::DateTime
66
def serialize(value)
7-
super&.iso8601(9)
7+
super&.utc&.iso8601(9)
88
end
99

1010
def cast_value(value)
11-
super&.to_datetime
11+
super&.to_time
1212
end
1313
end
1414
end

lib/kredis/type/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def type
88
end
99

1010
def cast_value(value)
11-
JSON.load(value)
11+
JSON.parse(value)
1212
end
1313

1414
def serialize(value)

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: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Kredis::Types::List < Kredis::Types::Proxying
2-
proxying :lrange, :lrem, :lpush, :rpush, :exists?, :del
2+
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del
33

44
attr_accessor :typed
55

@@ -8,20 +8,24 @@ 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

2424
def clear
2525
del
2626
end
27+
28+
def last(n = nil)
29+
n ? lrange(-n, -1) : lrange(-1, -1).first
30+
end
2731
end

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

0 commit comments

Comments
 (0)