kredis 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdd8725b73c1bac9541b7b48ee62ebb46a8c51eaf78abe367fda75a12ee8c425
4
- data.tar.gz: fcee76b43f62b3bd0a99fe7d258cb1b5edab0a8d7f408f15f7e11be01a40b219
3
+ metadata.gz: 72d301175c0d6bac257d10d925bddcaaa4d9fda78f0aa75264fc3b6c5a8af677
4
+ data.tar.gz: aa7dd573678ca4f8bb18478cf6b606fe2afc3833e6c9b9bb9b693e361f7706ee
5
5
  SHA512:
6
- metadata.gz: 1bd30168c4efb0e23f65f15eb0e0b34c0b3a62a6c871da6be82192187482f0fddc55405ffb52c12f8dad4397246b55f7d292aa3bbbd8d3dc75a4fdd2e94e009e
7
- data.tar.gz: 7dc32845d42347bcabe655447174d9cfb8b5e1d738fdf59af96557a455dc55ca52fced20fd76e514a21628dd725312120cbb6f0577e7925d6a6da585e77ffbf8
6
+ metadata.gz: e12490fc43dd3e73dcc39dfe3af5ab0f44405eae7a071b08edb14b1cba08c4b96bdb968af61072b2329e0f5e449c8c281252f4e32f423656d77bcc5ebf0e95e0
7
+ data.tar.gz: f047ac1e52365a7b25e10b2d2187df699abe34ced746f4d34b8e47511c019019eee3e9e6f69a5e46d58286a5ad5e38899d0db8517015d809b278ea46526f81b4
data/README.md CHANGED
@@ -62,6 +62,14 @@ unique_list << "5" # => LREM myuniquelist 0, "5" + R
62
62
  unique_list.remove(3) # => LREM myuniquelist 0, "3"
63
63
  [ "4", "2", "1", "5" ] == unique_list.elements # => LRANGE myuniquelist 0, -1
64
64
 
65
+ ordered_set = Kredis.ordered_set "myorderedset"
66
+ ordered_set.append(%w[ 2 3 4 ]) # => ZADD myorderedset 1646131025.4953232 2 1646131025.495326 3 1646131025.4953272 4
67
+ ordered_set.prepend(%w[ 1 2 3 4 ]) # => ZADD myorderedset -1646131025.4957051 1 -1646131025.495707 2 -1646131025.4957082 3 -1646131025.4957092 4
68
+ ordered_set.append([])
69
+ ordered_set << "5" # => ZADD myorderedset 1646131025.4960442 5
70
+ ordered_set.remove(3) # => ZREM myorderedset 3
71
+ [ "4", "2", "1", "5" ] == ordered_set.elements # => ZRANGE myorderedset 0 -1
72
+
65
73
  set = Kredis.set "myset", typed: :datetime
66
74
  set.add(DateTime.tomorrow, DateTime.yesterday) # => SADD myset "2021-02-03 00:00:00 +0100" "2021-02-01 00:00:00 +0100"
67
75
  set << DateTime.tomorrow # => SADD myset "2021-02-03 00:00:00 +0100"
@@ -33,10 +33,10 @@ class Kredis::Migration
33
33
  def delete_all(*key_patterns)
34
34
  log_migration "DELETE ALL #{key_patterns.inspect}" do
35
35
  if key_patterns.length > 1
36
- @redis.del *key_patterns
36
+ @redis.del(*key_patterns)
37
37
  else
38
38
  each_key_batch_matching(key_patterns.first) do |keys, pipeline|
39
- pipeline.del *keys
39
+ pipeline.del(*keys)
40
40
  end
41
41
  end
42
42
  end
@@ -0,0 +1,75 @@
1
+ class Kredis::Types::OrderedSet < Kredis::Types::Proxying
2
+ proxying :multi, :zrange, :zrem, :zadd, :zremrangebyrank, :zcard, :exists?, :del
3
+
4
+ attr_accessor :typed
5
+ attr_reader :limit
6
+
7
+ def elements
8
+ strings_to_types(zrange(0, -1) || [], typed)
9
+ end
10
+ alias to_a elements
11
+
12
+ def remove(*elements)
13
+ zrem(types_to_strings(elements, typed))
14
+ end
15
+
16
+ def prepend(elements)
17
+ insert(elements, prepending: true)
18
+ end
19
+
20
+ def append(elements)
21
+ insert(elements)
22
+ end
23
+ alias << append
24
+
25
+ def limit=(limit)
26
+ raise "Limit must be greater than 0" if limit && limit <= 0
27
+
28
+ @limit = limit
29
+ end
30
+
31
+ private
32
+ def insert(elements, prepending: false)
33
+ elements = Array(elements)
34
+ return if elements.empty?
35
+
36
+ elements_with_scores = types_to_strings(elements, typed).map.with_index do |element, index|
37
+ incremental_score = index * 0.000001
38
+
39
+ score = if prepending
40
+ -base_score - incremental_score
41
+ else
42
+ base_score + incremental_score
43
+ end
44
+
45
+ [ score , element ]
46
+ end
47
+
48
+ multi do
49
+ zadd(elements_with_scores)
50
+ trim(from_beginning: prepending)
51
+ end
52
+ end
53
+
54
+ def base_score
55
+ process_start_time + process_uptime
56
+ end
57
+
58
+ def process_start_time
59
+ @process_start_time ||= unproxied_redis.time.join(".").to_f - process_uptime
60
+ end
61
+
62
+ def process_uptime
63
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
64
+ end
65
+
66
+ def trim(from_beginning:)
67
+ return unless limit
68
+
69
+ if from_beginning
70
+ zremrangebyrank(limit, -1)
71
+ else
72
+ zremrangebyrank(0, -(limit + 1))
73
+ end
74
+ end
75
+ end
@@ -4,10 +4,11 @@ class Kredis::Types::Proxying
4
4
  attr_accessor :proxy, :key
5
5
 
6
6
  def self.proxying(*commands)
7
- delegate *commands, to: :proxy
7
+ delegate(*commands, to: :proxy)
8
8
  end
9
9
 
10
10
  def initialize(redis, key, **options)
11
+ @redis = redis
11
12
  @key = key
12
13
  @proxy = Kredis::Types::Proxy.new(redis, key)
13
14
  options.each { |key, value| send("#{key}=", value) }
@@ -17,6 +18,12 @@ class Kredis::Types::Proxying
17
18
  proxy.suppress_failsafe_with(returning: returning, &block)
18
19
  end
19
20
 
21
+ def unproxied_redis
22
+ # Generally, this should not be used. It's only here for the rare case where we need to
23
+ # call Redis commands that don't reference a key and don't want to be pipelined.
24
+ @redis
25
+ end
26
+
20
27
  private
21
28
  delegate :type_to_string, :string_to_type, :types_to_strings, :strings_to_types, to: :Kredis
22
29
  end
@@ -22,7 +22,7 @@ class Kredis::Types::UniqueList < Kredis::Types::List
22
22
  multi do
23
23
  remove elements
24
24
  super
25
- ltrim -limit, -1 if limit
25
+ ltrim(-limit, -1) if limit
26
26
  end
27
27
  end
28
28
  alias << append
data/lib/kredis/types.rb CHANGED
@@ -71,6 +71,10 @@ module Kredis::Types
71
71
  type_from(Set, config, key, after_change: after_change, typed: typed)
72
72
  end
73
73
 
74
+ def ordered_set(key, typed: :string, limit: nil, config: :shared, after_change: nil)
75
+ type_from(OrderedSet, config, key, after_change: after_change, typed: typed, limit: limit)
76
+ end
77
+
74
78
  def slot(key, config: :shared, after_change: nil)
75
79
  type_from(Slots, config, key, after_change: after_change, available: 1)
76
80
  end
@@ -99,4 +103,5 @@ require "kredis/types/hash"
99
103
  require "kredis/types/list"
100
104
  require "kredis/types/unique_list"
101
105
  require "kredis/types/set"
106
+ require "kredis/types/ordered_set"
102
107
  require "kredis/types/slots"
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.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.4.0
4
+ version: 1.5.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: 2023-06-18 00:00:00.000000000 Z
12
+ date: 2023-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -101,6 +101,7 @@ files:
101
101
  - lib/kredis/types/flag.rb
102
102
  - lib/kredis/types/hash.rb
103
103
  - lib/kredis/types/list.rb
104
+ - lib/kredis/types/ordered_set.rb
104
105
  - lib/kredis/types/proxy.rb
105
106
  - lib/kredis/types/proxy/failsafe.rb
106
107
  - lib/kredis/types/proxying.rb