kredis 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kredis might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4960df7b832b98970196c71cff95825187bbc49e7457ee13436a5408b841ef96
4
- data.tar.gz: 3e6bf39db78519fd2a15e405a46d59cc20d0b789581487f968e7b32efd4c173f
3
+ metadata.gz: ef7140930b42a8d19b49568c233c1b0c9b1e799f5d8d6948e96442a2bc74e7c1
4
+ data.tar.gz: 205fc23eb761dba14f10ffd52506ad748b0a19c2799d75a059841d17fbe9080e
5
5
  SHA512:
6
- metadata.gz: 4f5bf3d8078530a6a28a2f4f34e85f384ea3e452777902831b0bb144d28dfaa7d95c2bf7d8646c4e1eab427d5f3c293ef351eb069ca23f6bebce8a644ba6df85
7
- data.tar.gz: b6a0eaa250c74167473ade0f3ea77636d19faf3dd0c26a563fb22f3adf9602d98fb159fb352a188ad1abb08f3b5f6c805a1fd5657f4b16e3484b4a8703ed3839
6
+ metadata.gz: e95afce53e43f5ee10f527f41854a058ccd98c34d0d52394d99addcd0e774409c5aa1bf26d06e291fc12a3f5e7e50fffdffc686ed26e2926fe36709a08da90be
7
+ data.tar.gz: c03d6654a1cdb7680bad60efd2287b82f4ac720362d3c65935629beeeb466c54ff08341e5016173dca19ac1bb67cf48de52c296ddfd3a17fbb6d230090c6b38a
data/README.md CHANGED
@@ -109,8 +109,10 @@ enum = Kredis.enum "myenum", values: %w[ one two three ], default: "one"
109
109
  true == enum.one? # => GET myenum
110
110
  enum.value = "two" # => SET myenum "two"
111
111
  "two" == enum.value # => GET myenum
112
+ enum.three! # => SET myenum "three"
113
+ "three" == enum.value # => GET myenum
112
114
  enum.value = "four"
113
- "two" == enum.value # => GET myenum
115
+ "three" == enum.value # => GET myenum
114
116
  enum.reset # => DEL myenum
115
117
  "one" == enum.value # => GET myenum
116
118
 
@@ -168,10 +170,16 @@ You can use all these structures in models:
168
170
  ```ruby
169
171
  class Person < ApplicationRecord
170
172
  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
172
175
  kredis_unique_list :skills, limit: 2
173
176
  kredis_enum :morning, values: %w[ bright blue black ], default: "bright"
174
177
  kredis_counter :steps, expires_in: 1.hour
178
+
179
+ private
180
+ def generate_names_key
181
+ "key-generated-from-private-method"
182
+ end
175
183
  end
176
184
 
177
185
  person = Person.find(5)
@@ -195,14 +203,17 @@ end
195
203
 
196
204
  ## Installation
197
205
 
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)
201
208
 
202
209
  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`.
203
210
 
204
211
  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.
205
212
 
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
+
206
217
  ### Setting SSL options on Redis Connections
207
218
 
208
219
  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:
@@ -97,6 +97,7 @@ module Kredis::Attributes
97
97
  case key
98
98
  when String then key
99
99
  when Proc then key.call(self)
100
+ when Symbol then send(key)
100
101
  end
101
102
  end
102
103
 
@@ -10,20 +10,20 @@ class Kredis::Migration
10
10
  end
11
11
 
12
12
  def migrate_all(key_pattern)
13
- each_key_batch_matching(key_pattern) do |keys|
13
+ each_key_batch_matching(key_pattern) do |keys, pipeline|
14
14
  keys.each do |key|
15
15
  ids = key.scan(/\d+/).map(&:to_i)
16
- migrate from: key, to: yield(key, *ids)
16
+ migrate from: key, to: yield(key, *ids), pipeline: pipeline
17
17
  end
18
18
  end
19
19
  end
20
20
 
21
- def migrate(from:, to:)
21
+ def migrate(from:, to:, pipeline: nil)
22
22
  namespaced_to = Kredis.namespaced_key(to)
23
23
 
24
24
  if to.present? && from != namespaced_to
25
25
  log_migration "Migrating key #{from} to #{namespaced_to}" do
26
- @redis.evalsha @copy_sha, keys: [ from, namespaced_to ]
26
+ (pipeline || @redis).evalsha @copy_sha, keys: [ from, namespaced_to ]
27
27
  end
28
28
  else
29
29
  log_migration "Skipping blank/unaltered migration key #{from} → #{to}"
@@ -31,8 +31,8 @@ class Kredis::Migration
31
31
  end
32
32
 
33
33
  def delete_all(key_pattern)
34
- each_key_batch_matching(key_pattern) do |keys|
35
- @redis.del *keys
34
+ each_key_batch_matching(key_pattern) do |keys, pipeline|
35
+ pipeline.del *keys
36
36
  end
37
37
  end
38
38
 
@@ -43,7 +43,7 @@ class Kredis::Migration
43
43
  cursor = "0"
44
44
  begin
45
45
  cursor, keys = @redis.scan(cursor, match: key_pattern, count: SCAN_BATCH_SIZE)
46
- @redis.pipelined { yield keys }
46
+ @redis.multi { |pipeline| yield keys, pipeline }
47
47
  end until cursor == "0"
48
48
  end
49
49
 
@@ -4,7 +4,7 @@ class Kredis::Types::CallbacksProxy
4
4
 
5
5
  AFTER_CHANGE_OPERATIONS = {
6
6
  Kredis::Types::Counter => %i[ increment decrement reset ],
7
- Kredis::Types::Cycle => %i[ next ],
7
+ Kredis::Types::Cycle => %i[ next reset ],
8
8
  Kredis::Types::Enum => %i[ value= reset ],
9
9
  Kredis::Types::Flag => %i[ mark remove ],
10
10
  Kredis::Types::Hash => %i[ update delete []= remove ],
@@ -4,17 +4,17 @@ class Kredis::Types::Counter < Kredis::Types::Proxying
4
4
  attr_accessor :expires_in
5
5
 
6
6
  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]
11
11
  end
12
12
 
13
13
  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]
18
18
  end
19
19
 
20
20
  def value
@@ -28,6 +28,7 @@ class Kredis::Types::Enum < Kredis::Types::Proxying
28
28
  def define_predicates_for_values
29
29
  values.each do |defined_value|
30
30
  define_singleton_method("#{defined_value}?") { value == defined_value }
31
+ define_singleton_method("#{defined_value}!") { self.value = defined_value }
31
32
  end
32
33
  end
33
34
  end
@@ -8,16 +8,16 @@ class Kredis::Types::List < Kredis::Types::Proxying
8
8
  end
9
9
  alias to_a elements
10
10
 
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 }
13
13
  end
14
14
 
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?
17
17
  end
18
18
 
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?
21
21
  end
22
22
  alias << append
23
23
 
@@ -9,8 +9,13 @@ class Kredis::Types::Proxy
9
9
  options.each { |key, value| send("#{key}=", value) }
10
10
  end
11
11
 
12
- def multi(...)
13
- redis.multi(...)
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))
18
+ end
14
19
  end
15
20
 
16
21
  def method_missing(method, *args, **kwargs)
@@ -8,19 +8,19 @@ class Kredis::Types::Set < Kredis::Types::Proxying
8
8
  end
9
9
  alias to_a members
10
10
 
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?
13
13
  end
14
14
  alias << add
15
15
 
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?
18
18
  end
19
19
 
20
20
  def replace(*members)
21
- multi do
22
- del
23
- add members
21
+ multi do |pipeline|
22
+ pipeline.del
23
+ add members, pipeline: pipeline
24
24
  end
25
25
  end
26
26
 
@@ -19,10 +19,10 @@ class Kredis::Types::Slots < Kredis::Types::Proxying
19
19
  release
20
20
  end
21
21
  else
22
- if incr <= available
22
+ if available?
23
+ incr
23
24
  true
24
25
  else
25
- release
26
26
  false
27
27
  end
28
28
  end
@@ -30,16 +30,25 @@ class Kredis::Types::Slots < Kredis::Types::Proxying
30
30
  end
31
31
 
32
32
  def release
33
- decr
33
+ if taken > 0
34
+ decr
35
+ true
36
+ else
37
+ false
38
+ end
34
39
  end
35
40
 
36
41
  def available?
37
42
  failsafe returning: false do
38
- get.to_i < available
43
+ taken < available
39
44
  end
40
45
  end
41
46
 
42
47
  def reset
43
48
  del
44
49
  end
50
+
51
+ def taken
52
+ get.to_i
53
+ end
45
54
  end
@@ -8,10 +8,10 @@ class Kredis::Types::UniqueList < Kredis::Types::List
8
8
  elements = Array(elements).uniq
9
9
  return if elements.empty?
10
10
 
11
- multi do
12
- remove elements
13
- super
14
- ltrim 0, (limit - 1) if limit
11
+ multi do |pipeline|
12
+ remove elements, pipeline: pipeline
13
+ super(elements, pipeline: pipeline)
14
+ pipeline.ltrim 0, (limit - 1) if limit
15
15
  end
16
16
  end
17
17
 
@@ -19,10 +19,10 @@ class Kredis::Types::UniqueList < Kredis::Types::List
19
19
  elements = Array(elements).uniq
20
20
  return if elements.empty?
21
21
 
22
- multi do
23
- remove elements
24
- super
25
- ltrim -limit, -1 if limit
22
+ multi do |pipeline|
23
+ remove elements, pipeline: pipeline
24
+ super(elements, pipeline: pipeline)
25
+ pipeline.ltrim -limit, -1 if limit
26
26
  end
27
27
  end
28
28
  alias << append
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kredis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Timm Hansen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-02-19 00:00:00.000000000 Z
12
+ date: 2022-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport