Skip to content

Commit 83619db

Browse files
committed
Merge branch 'main' into lb/resolve-pipelining-deprecation-warnings
* main: Bump version for 1.2.0 Use pipeline for migration too Ensure to pass the pipeline to super Use block parameter to pipeline in Redis#multi (#68) Note tested / supported versions of Redis in the Readme (#79) Return counter value after incrementing/decrementing Enum Bang setter (#82) Add reset to Cycle after_change callbacks Add example in Readme.md Support configuring custom keys via method invocation Use bundle add instead (#81)
2 parents e1425dc + f164b42 commit 83619db

File tree

16 files changed

+101
-54
lines changed

16 files changed

+101
-54
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ jobs:
55
runs-on: ubuntu-latest
66
strategy:
77
matrix:
8-
ruby: [2.7, "3.0"]
8+
redis_server: ["4", "5", "6.2"]
9+
ruby: ["2.7", "3.0"]
10+
11+
name: Redis server ${{ matrix.redis_server }} - Ruby ${{ matrix.ruby }}
912

1013
steps:
1114
- uses: actions/checkout@v2
@@ -16,10 +19,10 @@ jobs:
1619
ruby-version: ${{ matrix.ruby }}
1720
bundler-cache: true
1821

19-
- name: Set up Redis 4
22+
- name: Set up Redis ${{ matrix.redis_server }}
2023
uses: supercharge/[email protected]
2124
with:
22-
redis-version: 4
25+
redis-version: ${{ matrix.redis_server }}
2326

2427
- name: Run tests
2528
run: bin/test

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
kredis (1.1.0)
4+
kredis (1.2.0)
55
activesupport (>= 6.0.0)
66
redis (~> 4.2)
77

@@ -153,4 +153,4 @@ DEPENDENCIES
153153
rake
154154

155155
BUNDLED WITH
156-
2.3.4
156+
2.3.12

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ enum = Kredis.enum "myenum", values: %w[ one two three ], default: "one"
109109
true == enum.one? # => GET myenum
110110
enum.value = "two" # => SET myenum "two"
111111
"two" == enum.value # => GET myenum
112+
enum.three! # => SET myenum "three"
113+
"three" == enum.value # => GET myenum
112114
enum.value = "four"
113-
"two" == enum.value # => GET myenum
115+
"three" == enum.value # => GET myenum
114116
enum.reset # => DEL myenum
115117
"one" == enum.value # => GET myenum
116118

@@ -168,10 +170,16 @@ You can use all these structures in models:
168170
```ruby
169171
class Person < ApplicationRecord
170172
kredis_list :names
171-
kredis_list :names_with_custom_key, key: ->(p) { "person:#{p.id}:names_customized" }
173+
kredis_list :names_with_custom_key_via_lambda, key: ->(p) { "person:#{p.id}:names_customized" }
174+
kredis_list :names_with_custom_key_via_method, key: :generate_names_key
172175
kredis_unique_list :skills, limit: 2
173176
kredis_enum :morning, values: %w[ bright blue black ], default: "bright"
174177
kredis_counter :steps, expires_in: 1.hour
178+
179+
private
180+
def generate_names_key
181+
"key-generated-from-private-method"
182+
end
175183
end
176184

177185
person = Person.find(5)
@@ -195,14 +203,17 @@ end
195203

196204
## Installation
197205

198-
1. Add the `kredis` gem to your Gemfile: `gem 'kredis'`
199-
2. Run `./bin/bundle install`
200-
3. Run `./bin/rails kredis:install` to add a default configuration at [`config/redis/shared.yml`](lib/install/shared.yml)
206+
1. Run `./bin/bundle add kredis`
207+
2. Run `./bin/rails kredis:install` to add a default configuration at [`config/redis/shared.yml`](lib/install/shared.yml)
201208

202209
Additional configurations can be added under `config/redis/*.yml` and referenced when a type is created. For example, `Kredis.string("mystring", config: :strings)` would lookup `config/redis/strings.yml`.
203210

204211
Kredis passes the configuration to `Redis.new` to establish the connection. See the [Redis documentation](https://github.com/redis/redis-rb) for other configuration options.
205212

213+
### Redis support
214+
215+
Kredis works with Redis server 4.0+, with the [Redis Ruby](https://github.com/redis/redis-rb) client version 4.2+.
216+
206217
### Setting SSL options on Redis Connections
207218

208219
If you need to connect to Redis with SSL, the recommended approach is to set your Redis instance manually by adding an entry to the `Kredis::Connections.connections` hash. Below an example showing how to connect to Redis using Client Authentication:

lib/kredis/attributes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def kredis_key_evaluated(key)
9797
case key
9898
when String then key
9999
when Proc then key.call(self)
100+
when Symbol then send(key)
100101
end
101102
end
102103

lib/kredis/migration.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@ def initialize(config = :shared)
1111
end
1212

1313
def migrate_all(key_pattern)
14-
each_key_batch_matching(key_pattern) do |keys|
14+
each_key_batch_matching(key_pattern) do |keys, pipeline|
1515
keys.each do |key|
1616
ids = key.scan(/\d+/).map(&:to_i)
17-
migrate from: key, to: yield(key, *ids)
17+
migrate from: key, to: yield(key, *ids), pipeline: pipeline
1818
end
1919
end
2020
end
2121

22-
def migrate(from:, to:)
22+
def migrate(from:, to:, pipeline: nil)
2323
namespaced_to = Kredis.namespaced_key(to)
2424

2525
if to.present? && from != namespaced_to
2626
log_migration "Migrating key #{from} to #{namespaced_to}" do
27-
connection.evalsha @copy_sha, keys: [ from, namespaced_to ]
27+
(pipeline || @redis).evalsha @copy_sha, keys: [ from, namespaced_to ]
2828
end
2929
else
3030
log_migration "Skipping blank/unaltered migration key #{from}#{to}"
3131
end
3232
end
3333

3434
def delete_all(key_pattern)
35-
each_key_batch_matching(key_pattern) do |keys|
36-
connection.del *keys
35+
each_key_batch_matching(key_pattern) do |keys, pipeline|
36+
pipeline.del *keys
3737
end
3838
end
3939

@@ -48,11 +48,7 @@ def each_key_batch_matching(key_pattern, &block)
4848
cursor = "0"
4949
begin
5050
cursor, keys = @redis.scan(cursor, match: key_pattern, count: SCAN_BATCH_SIZE)
51-
@redis.pipelined do |pipeline|
52-
@pipeline = pipeline
53-
yield keys
54-
@pipeline = nil
55-
end
51+
@redis.multi { |pipeline| yield keys, pipeline }
5652
end until cursor == "0"
5753
end
5854

lib/kredis/types/callbacks_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Kredis::Types::CallbacksProxy
44

55
AFTER_CHANGE_OPERATIONS = {
66
Kredis::Types::Counter => %i[ increment decrement reset ],
7-
Kredis::Types::Cycle => %i[ next ],
7+
Kredis::Types::Cycle => %i[ next reset ],
88
Kredis::Types::Enum => %i[ value= reset ],
99
Kredis::Types::Flag => %i[ mark remove ],
1010
Kredis::Types::Hash => %i[ update delete []= remove ],

lib/kredis/types/counter.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ class Kredis::Types::Counter < Kredis::Types::Proxying
44
attr_accessor :expires_in
55

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

1313
def decrement(by: 1)
14-
multi do
15-
set 0, ex: expires_in, nx: true
16-
decrby by
17-
end
14+
multi do |pipeline|
15+
pipeline.set 0, ex: expires_in, nx: true
16+
pipeline.decrby by
17+
end[-1]
1818
end
1919

2020
def value

lib/kredis/types/enum.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def reset
2828
def define_predicates_for_values
2929
values.each do |defined_value|
3030
define_singleton_method("#{defined_value}?") { value == defined_value }
31+
define_singleton_method("#{defined_value}!") { self.value = defined_value }
3132
end
3233
end
3334
end

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

lib/kredis/types/set.rb

Lines changed: 7 additions & 7 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)
21-
multi do
22-
del
23-
add members
21+
multi do |pipeline|
22+
pipeline.del
23+
add members, pipeline: pipeline
2424
end
2525
end
2626

0 commit comments

Comments
 (0)