mock_redis 0.40.0 → 0.41.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: 0a994c84019da05c17affd08708c56468251ad9c8667893b5841d57c238ae5ad
4
- data.tar.gz: 3d21509554037fc70d813de8265ea64c8dce549c8bbbb2d4872875e83a01fcb1
3
+ metadata.gz: 38600e39b89ea4f37c414a69fb7ed50e4915d0f723d2d7f50e11141b4f9f9e33
4
+ data.tar.gz: 5e2830a85a5dcf6326ed01c3cac52cf5c5136393dd79f979b9a197a79dd12c60
5
5
  SHA512:
6
- metadata.gz: 1db6f45379ac2252fd1024e8eecdf1a3f49289bd457cd63cb075c2baa19fdb9e1e775c537c7089e184e7d7e644d6d30b81f796516fa8bd3631bab623154e9531
7
- data.tar.gz: 5177f6c22cd0f7561ee9eaf46ebce4ba1f43f0c2b461fe87baa4c50dfe76ffeee05db2c5cc552e90431be44d01aa293ed6a672bd7a303ab16f0f6ee5659ea34e
6
+ metadata.gz: 1fcdf6c59782080a318f7915b918367aa7935f4de4dee130fac4bae158574e39685db1a81a598fae56d7c97a14e33c4b333c0e357375c45edd691c31bed20455
7
+ data.tar.gz: f0a336671d8a76ad0984604ad3f29ab231188442155de7968c1e0698489f5a02f3a012ab3ae3f38a8398c3d553cbc1ca68b9915514d6c5046ebede63a5c9e842
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # MockRedis Changelog
2
2
 
3
+ ### 0.41.0
4
+
5
+ * Add support for `expire`-related command options `nx`/`xx`/`lt`/`gt`
6
+
3
7
  ### 0.40.0
4
8
 
5
9
  * Add support for `call` method name to be case insensitive
@@ -43,6 +43,9 @@ class MockRedis
43
43
 
44
44
  # Redis commands go below this line and above 'private'
45
45
 
46
+ # FIXME: Current implementation of `call` does not work propetly with kwarg-options.
47
+ # i.e. `call("EXPIRE", "foo", 40, "NX")` (which redis-rb will simply transmit to redis-server)
48
+ # will be passed to `#expire` without keywords transformation.
46
49
  def call(command, &_block)
47
50
  public_send(command[0].downcase, *command[1..])
48
51
  end
@@ -89,32 +92,42 @@ class MockRedis
89
92
  msg.to_s
90
93
  end
91
94
 
92
- def expire(key, seconds)
95
+ def expire(key, seconds, nx: nil, xx: nil, lt: nil, gt: nil) # rubocop:disable Metrics/ParameterLists
93
96
  assert_valid_integer(seconds)
94
97
 
95
- pexpire(key, seconds.to_i * 1000)
98
+ pexpire(key, seconds.to_i * 1000, nx: nx, xx: xx, lt: lt, gt: gt)
96
99
  end
97
100
 
98
- def pexpire(key, ms)
101
+ def pexpire(key, ms, nx: nil, xx: nil, lt: nil, gt: nil) # rubocop:disable Metrics/ParameterLists
99
102
  assert_valid_integer(ms)
100
103
 
101
104
  now, miliseconds = @base.now
102
105
  now_ms = (now * 1000) + miliseconds
103
- pexpireat(key, now_ms + ms.to_i)
106
+ pexpireat(key, now_ms + ms.to_i, nx: nx, xx: xx, lt: lt, gt: gt)
104
107
  end
105
108
 
106
- def expireat(key, timestamp)
109
+ def expireat(key, timestamp, nx: nil, xx: nil, lt: nil, gt: nil) # rubocop:disable Metrics/ParameterLists
107
110
  assert_valid_integer(timestamp)
108
111
 
109
- pexpireat(key, timestamp.to_i * 1000)
112
+ pexpireat(key, timestamp.to_i * 1000, nx: nx, xx: xx, lt: lt, gt: gt)
110
113
  end
111
114
 
112
- def pexpireat(key, timestamp_ms)
115
+ def pexpireat(key, timestamp_ms, nx: nil, xx: nil, lt: nil, gt: nil) # rubocop:disable Metrics/ParameterLists
113
116
  assert_valid_integer(timestamp_ms)
114
117
 
115
- if exists?(key)
116
- timestamp = Rational(timestamp_ms.to_i, 1000)
117
- set_expiration(key, @base.time_at(timestamp))
118
+ if nx && gt || gt && lt || lt && nx || nx && xx
119
+ raise Redis::CommandError, <<~TXT.chomp
120
+ ERR NX and XX, GT or LT options at the same time are not compatible
121
+ TXT
122
+ end
123
+
124
+ return false unless exists?(key)
125
+
126
+ expiry = expiration(key)
127
+ new_expiry = @base.time_at(Rational(timestamp_ms.to_i, 1000))
128
+
129
+ if should_update_expiration?(expiry, new_expiry, nx: nx, xx: xx, lt: lt, gt: gt)
130
+ set_expiration(key, new_expiry)
118
131
  true
119
132
  else
120
133
  false
@@ -324,7 +337,7 @@ class MockRedis
324
337
  end
325
338
 
326
339
  def expiration(key)
327
- expire_times.find { |(_, k)| k == key.to_s }.first
340
+ expire_times.find { |(_, k)| k == key.to_s }&.first
328
341
  end
329
342
 
330
343
  def has_expiration?(key)
@@ -339,6 +352,14 @@ class MockRedis
339
352
  !!Float(str) rescue false
340
353
  end
341
354
 
355
+ def should_update_expiration?(expiry, new_expiry, nx:, xx:, lt:, gt:) # rubocop:disable Metrics/ParameterLists
356
+ return false if nx && expiry || xx && !expiry
357
+ return false if lt && expiry && new_expiry > expiry
358
+ return false if gt && (!expiry || new_expiry < expiry)
359
+
360
+ true
361
+ end
362
+
342
363
  def redis_pattern_to_ruby_regex(pattern)
343
364
  Regexp.new(
344
365
  "^#{pattern}$".
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  class MockRedis
5
- VERSION = '0.40.0'
5
+ VERSION = '0.41.0'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.40.0
4
+ version: 0.41.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-11-16 00:00:00.000000000 Z
12
+ date: 2023-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -123,10 +123,10 @@ licenses:
123
123
  - MIT
124
124
  metadata:
125
125
  bug_tracker_uri: https://github.com/sds/mock_redis/issues
126
- changelog_uri: https://github.com/sds/mock_redis/blob/v0.40.0/CHANGELOG.md
127
- documentation_uri: https://www.rubydoc.info/gems/mock_redis/0.40.0
126
+ changelog_uri: https://github.com/sds/mock_redis/blob/v0.41.0/CHANGELOG.md
127
+ documentation_uri: https://www.rubydoc.info/gems/mock_redis/0.41.0
128
128
  homepage_uri: https://github.com/sds/mock_redis
129
- source_code_uri: https://github.com/sds/mock_redis/tree/v0.40.0
129
+ source_code_uri: https://github.com/sds/mock_redis/tree/v0.41.0
130
130
  post_install_message:
131
131
  rdoc_options: []
132
132
  require_paths: