kredis 1.3.0.1 → 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: 6042f4320cf89f8b0eae1ce4c8daa61ee6821775b7fdeab02777673a25b2cba1
4
- data.tar.gz: 67121a89444ffd7f8e0a1636fc6651a2851bbc5b63514a23413136bca2abd22d
3
+ metadata.gz: 72d301175c0d6bac257d10d925bddcaaa4d9fda78f0aa75264fc3b6c5a8af677
4
+ data.tar.gz: aa7dd573678ca4f8bb18478cf6b606fe2afc3833e6c9b9bb9b693e361f7706ee
5
5
  SHA512:
6
- metadata.gz: 81888671af0fb781788d890640142da0f36bf03589d628ec2aaeca1d86608d9602282a9b9955e0c496cd47be01d4f4e3e42d47ac9d9414949ca635c2768177b8
7
- data.tar.gz: eb7f0f1896bf2c27c4fa5630d43db65c8fcf183de9be5709146a3d06d183afdc33873f705a57dbbea5c58c2ca826758a82bd84ef81dfd320ca1d2a5c1c5eef1a
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"
@@ -15,7 +15,7 @@ class Kredis::LogSubscriber < ActiveSupport::LogSubscriber
15
15
 
16
16
  private
17
17
  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
19
19
  end
20
20
  end
21
21
 
@@ -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
@@ -4,11 +4,11 @@ module Kredis
4
4
  module Type
5
5
  class DateTime < ActiveModel::Type::DateTime
6
6
  def serialize(value)
7
- super&.iso8601(9)
7
+ super&.utc&.iso8601(9)
8
8
  end
9
9
 
10
10
  def cast_value(value)
11
- super&.to_datetime
11
+ super&.to_time
12
12
  end
13
13
  end
14
14
  end
@@ -24,4 +24,8 @@ class Kredis::Types::List < Kredis::Types::Proxying
24
24
  def clear
25
25
  del
26
26
  end
27
+
28
+ def last(n = nil)
29
+ n ? lrange(-n, -1) : lrange(-1, -1).first
30
+ end
27
31
  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
@@ -1,5 +1,5 @@
1
1
  class Kredis::Types::Set < Kredis::Types::Proxying
2
- proxying :smembers, :sadd, :srem, :multi, :del, :sismember, :scard, :spop, :exists?
2
+ proxying :smembers, :sadd, :srem, :multi, :del, :sismember, :scard, :spop, :exists?, :srandmember
3
3
 
4
4
  attr_accessor :typed
5
5
 
@@ -33,10 +33,18 @@ class Kredis::Types::Set < Kredis::Types::Proxying
33
33
  end
34
34
 
35
35
  def take
36
- spop
36
+ string_to_type(spop, typed)
37
37
  end
38
38
 
39
39
  def clear
40
40
  del
41
41
  end
42
+
43
+ def sample(count = nil)
44
+ if count.nil?
45
+ string_to_type(srandmember(count), typed)
46
+ else
47
+ strings_to_types(srandmember(count), typed)
48
+ end
49
+ end
42
50
  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.3.0.1"
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.3.0.1
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-03-13 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
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: 6.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: activemodel
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 6.0.0
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 6.0.0
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: redis
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +101,7 @@ files:
87
101
  - lib/kredis/types/flag.rb
88
102
  - lib/kredis/types/hash.rb
89
103
  - lib/kredis/types/list.rb
104
+ - lib/kredis/types/ordered_set.rb
90
105
  - lib/kredis/types/proxy.rb
91
106
  - lib/kredis/types/proxy/failsafe.rb
92
107
  - lib/kredis/types/proxying.rb
@@ -115,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
130
  - !ruby/object:Gem::Version
116
131
  version: '0'
117
132
  requirements: []
118
- rubygems_version: 3.4.2
133
+ rubygems_version: 3.4.10
119
134
  signing_key:
120
135
  specification_version: 4
121
136
  summary: Higher-level data structures built on Redis.