kredis 0.2.0 → 0.2.1

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: 9aa90994f3d30a19d52df3fbc284d43d2637e940de06f221ecffc2853eeda459
4
- data.tar.gz: 713137c0ec2c28bfb80df9c94b3e61c4b8f5906c9d68631fd5e26e4ac8ef0771
3
+ metadata.gz: 8f1ad5de0aa1b64c083109609ec185ea81e35b3550fced4347f5bff12ff35832
4
+ data.tar.gz: 85d287eb15524b0b668524a51820ffb3a64d4bccda398f07802abb5867b49b92
5
5
  SHA512:
6
- metadata.gz: afcbb020c7e7c412090a7798c433882adf7d54e293728eff3a7d39b655d41531a3bae5f307ea10faa72a39e6ed1cb1f7db1f3f6535351299d26f65fd20e728fb
7
- data.tar.gz: 7de324aa868056600c28d82911ef654c7d9f58c5d85823ab1271a7518c76f9dbb31a2be23082cb9ab2bf210401eac0ceca698412a38f01779af310bcf5893624
6
+ metadata.gz: cfb7cfa924d9a6ecb79febf81cc2b2479694b0e275219d53e031b01fe2296a23862eee96f6359d05f33fc333808638b39c187c9f641252de3435488df3f6e372
7
+ data.tar.gz: cd06423690991d2a5364804e240aac27b88f792bc951623ce1e5f4f5a17946b817a05cfda9a4defb831e8e6c1d79ee0c3ab8523342d94c3a2259caca8609f1d9
data/README.md CHANGED
@@ -9,29 +9,41 @@ Kredis provides namespacing support for keys such that you can safely run parall
9
9
 
10
10
  ## Examples
11
11
 
12
- Kredis provides typed scalars for strings, integers, decimals, floats, booleans, datetimes, and json hashes:
12
+ Kredis provides typed scalars for strings, integers, decimals, floats, booleans, datetimes, and JSON hashes:
13
13
 
14
14
  ```ruby
15
15
  string = Kredis.string "mystring"
16
16
  string.value = "hello world!" # => SET mystring "hello world"
17
17
  "hello world!" == string.value # => GET mystring
18
18
 
19
- integer = Kredis.string "myinteger"
19
+ integer = Kredis.integer "myinteger"
20
20
  integer.value = 5 # => SET myinteger "5"
21
- 5 == string.value # => GET myinteger
21
+ 5 == integer.value # => GET myinteger
22
22
 
23
23
  json = Kredis.json "myjson"
24
- integer.value = { "one" => 1, "two" => "2" } # => SET myjson "{\"one\":1,\"two\":\"2\"}"
25
- { "one" => 1, "two" => "2" } == string.value # => GET myjson
24
+ json.value = { "one" => 1, "two" => "2" } # => SET myjson "{\"one\":1,\"two\":\"2\"}"
25
+ { "one" => 1, "two" => "2" } == json.value # => GET myjson
26
26
  ```
27
27
 
28
- There are data structures for counters, enums, flags, lists, uniqe lists, sets, and slots:
28
+ There are data structures for counters, enums, flags, lists, unique lists, sets, and slots:
29
29
 
30
30
  ```ruby
31
- list = Kredis.list "mylist", typed: :integer
32
- list.append([ 1, 2, 3 ]) # => LPUSH mylist "1" "2" "3"
33
- list << 4 # => LPUSH mylist "4"
34
- [ 1, 2, 3, 4 ] == list.elements # LRANGE 0 -1
31
+ list = Kredis.list "mylist"
32
+ list << "hello world!"
33
+ [ "hello world!" ] == list.elements
34
+
35
+ integer_list = Kredis.list "myintegerlist", typed: :integer
36
+ integer_list.append([ 1, 2, 3 ]) # => LPUSH myintegerlist "1" "2" "3"
37
+ integer_list << 4 # => LPUSH myintegerlist "4"
38
+ [ 1, 2, 3, 4 ] == integer_list.elements # LRANGE 0 -1
39
+
40
+ unique_list = Kredis.unique_list "myuniquelist"
41
+ unique_list.append(%w[ 2 3 4 ])
42
+ unique_list.prepend(%w[ 1 2 3 4 ])
43
+ unique_list.append([])
44
+ unique_list << "5"
45
+ unique_list.remove(3)
46
+ [ "1", "2", "4", "5" ] == unique_list.elements
35
47
 
36
48
  set = Kredis.set "myset", typed: :datetime
37
49
  set.add(DateTime.tomorrow, DateTime.yesterday) # => SADD myset "2021-02-03 00:00:00 +0100" "2021-02-01 00:00:00 +0100"
@@ -39,11 +51,66 @@ set << DateTime.tomorrow # => SADD myset "2021-
39
51
  2 == set.size # => SCARD myset
40
52
  [ DateTime.tomorrow, DateTime.yesterday ] == set.elements # => SMEMBERS myset
41
53
 
42
- counter = Kredis.counter "mycounter", expires_in: 15.minutes
54
+ head_count = Kredis.counter "headcount"
55
+ 0 == head_count.value # => GET "headcount"
56
+ head_count.increment
57
+ head_count.increment
58
+ head_count.decrement
59
+ 1 == head_count.value # => GET "headcount"
60
+
61
+ counter = Kredis.counter "mycounter", expires_in: 5.seconds
43
62
  counter.increment by: 2 # => SETEX "mycounter" 900 0 + INCR "mycounter" 2
44
63
  2 == counter.value # => GET "mycounter"
45
- travel 16.minutes
64
+ sleep 6.seconds
46
65
  0 == counter.value # => GET "mycounter"
66
+
67
+ enum = Kredis.enum "myenum", values: %w[ one two three ], default: "one"
68
+ "one" == enum.value
69
+ true == enum.one?
70
+ enum.value = "two"
71
+ "two" == enum.value
72
+ enum.value = "four"
73
+ "two" == enum.value
74
+ enum.reset
75
+ "one" == enum.value
76
+
77
+ slots = Kredis.slots "myslots", available: 3
78
+ true == slots.available?
79
+ slots.reserve
80
+ true == slots.available?
81
+ slots.reserve
82
+ true == slots.available?
83
+ slots.reserve
84
+ true == slots.available?
85
+ slots.reserve
86
+ false == slots.available?
87
+ slots.release
88
+ true == slots.available?
89
+ slots.reset
90
+
91
+ flag = Kredis.flag "myflag"
92
+ false == flag.marked?
93
+ flag.mark
94
+ true == flag.marked?
95
+ flag.remove
96
+ false == flag.marked?
97
+
98
+ flag.mark(expires_in: 1.second)
99
+ true == flag.marked?
100
+ sleep 0.5.seconds
101
+ true == flag.marked?
102
+ sleep 0.6.seconds
103
+ false == flag.marked?
104
+ ```
105
+
106
+ And using structures on a different than the default `shared` redis instance, relying on `config/redis/secondary.yml`:
107
+
108
+ ```ruby
109
+ one_string = Kredis.string "mystring"
110
+ two_string = Kredis.string "mystring", config: :secondary
111
+
112
+ one_string.value = "just on shared"
113
+ two_string.value != one_string.value
47
114
  ```
48
115
 
49
116
  You can use all these structures in models:
@@ -78,6 +78,10 @@ module Kredis::Attributes
78
78
  end
79
79
 
80
80
  def kredis_key_for_attribute(name)
81
- "#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}"
81
+ "#{self.class.name.tableize.gsub("/", ":")}:#{extract_kredis_id}:#{name}"
82
+ end
83
+
84
+ def extract_kredis_id
85
+ try(:id) or raise NotImplementedError, "kredis needs a unique id, either implement an id method or pass a custom key."
82
86
  end
83
87
  end
@@ -1,7 +1,7 @@
1
1
  require "active_support/core_ext/module/delegation"
2
2
 
3
3
  class Kredis::Migration
4
- singleton_class.delegate :migrate_all, :migrate, to: :new
4
+ singleton_class.delegate :migrate_all, :migrate, :delete_all, to: :new
5
5
 
6
6
  def initialize(config = :shared)
7
7
  @redis = Kredis.configured_for config
@@ -9,35 +9,39 @@ class Kredis::Migration
9
9
  end
10
10
 
11
11
  def migrate_all(key_pattern)
12
- find_keys_in_batches_matching(key_pattern) do |keys|
13
- @redis.multi do
14
- keys.each do |key|
15
- ids = key.scan(/\d+/).map(&:to_i)
16
- migrate from: key, to: yield(key, *ids)
17
- end
12
+ each_key_batch_matching(key_pattern) do |keys|
13
+ keys.each do |key|
14
+ ids = key.scan(/\d+/).map(&:to_i)
15
+ migrate from: key, to: yield(key, *ids)
18
16
  end
19
17
  end
20
18
  end
21
19
 
22
20
  def migrate(from:, to:)
23
- to = Kredis.namespaced_key(to)
21
+ namespaced_to = Kredis.namespaced_key(to)
24
22
 
25
- if from != to
26
- log_migration "Migrating key #{from} to #{to}"
27
- @redis.evalsha @copy_sha, keys: [ from, to ]
23
+ if to.present? && from != namespaced_to
24
+ log_migration "Migrating key #{from} to #{namespaced_to}"
25
+ @redis.evalsha @copy_sha, keys: [ from, namespaced_to ]
28
26
  else
29
- log_migration "Skipping unaltered migration key #{from}"
27
+ log_migration "Skipping blank/unaltered migration key #{from} → #{to}"
28
+ end
29
+ end
30
+
31
+ def delete_all(key_pattern)
32
+ each_key_batch_matching(key_pattern) do |keys|
33
+ @redis.del *keys
30
34
  end
31
35
  end
32
36
 
33
37
  private
34
38
  SCAN_BATCH_SIZE = 1_000
35
39
 
36
- def find_keys_in_batches_matching(key_pattern, &block)
40
+ def each_key_batch_matching(key_pattern, &block)
37
41
  cursor = "0"
38
42
  begin
39
43
  cursor, keys = @redis.scan(cursor, match: key_pattern, count: SCAN_BATCH_SIZE)
40
- yield keys
44
+ @redis.multi { yield keys }
41
45
  end until cursor == "0"
42
46
  end
43
47
 
@@ -20,9 +20,9 @@ module Kredis
20
20
  end
21
21
 
22
22
  initializer "kredis.attributes" do
23
- # TODO: Add run_load_hooks to ActiveModel::Model so this runs.
24
- ActiveSupport.on_load(:active_model) do
25
- include Kredis::Attributes
23
+ # No load hook for Active Model, just defer until after initialization.
24
+ config.after_initialize do
25
+ ActiveModel::Model.include Kredis::Attributes if defined?(ActiveModel::Model)
26
26
  end
27
27
 
28
28
  ActiveSupport.on_load(:active_record) do
@@ -1,5 +1,5 @@
1
1
  class Kredis::Types::Counter < Kredis::Types::Proxying
2
- proxying :multi, :set, :incrby, :get
2
+ proxying :multi, :set, :incrby, :decrby, :get
3
3
 
4
4
  attr_accessor :expires_in
5
5
 
@@ -10,6 +10,13 @@ class Kredis::Types::Counter < Kredis::Types::Proxying
10
10
  end
11
11
  end
12
12
 
13
+ def decrement(by: 1)
14
+ multi do
15
+ set 0, ex: expires_in, nx: true
16
+ decrby by
17
+ end
18
+ end
19
+
13
20
  def value
14
21
  get.to_i
15
22
  end
@@ -1,3 +1,5 @@
1
+ require "active_support/core_ext/enumerable"
2
+
1
3
  class Kredis::Types::Proxy
2
4
  attr_accessor :redis, :key
3
5
 
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
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: 0.2.0
4
+ version: 0.2.1
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: 2021-02-02 00:00:00.000000000 Z
12
+ date: 2021-02-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '4.0'
34
+ version: '4.2'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '4.0'
41
+ version: '4.2'
42
42
  description:
43
43
  email: david@hey.com
44
44
  executables: []
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - ">="
80
80
  - !ruby/object:Gem::Version
81
- version: 2.6.0
81
+ version: 2.7.0
82
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  requirements:
84
84
  - - ">="