protocol-redis 0.8.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,82 +4,83 @@
4
4
  # Copyright, 2019-2023, by Samuel Williams.
5
5
  # Copyright, 2021, by Troex Nevelin.
6
6
 
7
- require 'date'
7
+ require "date"
8
8
 
9
9
  module Protocol
10
10
  module Redis
11
11
  module Methods
12
+ # Methods for interacting with Redis keys.
12
13
  module Generic
13
14
  # Delete a key. O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).
14
- # @see https://redis.io/commands/del
15
- # @param key [Key]
15
+ # See <https://redis.io/commands/del> for more details.
16
+ # @parameter key [Key]
16
17
  def del(*keys)
17
18
  if keys.any?
18
- call('DEL', *keys)
19
+ call("DEL", *keys)
19
20
  end
20
21
  end
21
22
 
22
23
  # Return a serialized version of the value stored at the specified key. O(1) to access the key and additional O(N*M) to serialized it, where N is the number of Redis objects composing the value and M their average size. For small string values the time complexity is thus O(1)+O(1*M) where M is small, so simply O(1).
23
- # @see https://redis.io/commands/dump
24
- # @param key [Key]
24
+ # See <https://redis.io/commands/dump> for more details.
25
+ # @parameter key [Key]
25
26
  def dump(key)
26
- call('DUMP', key)
27
+ call("DUMP", key)
27
28
  end
28
29
 
29
30
  # Determine if a key exists. O(1).
30
- # @see https://redis.io/commands/exists
31
- # @param key [Key]
32
- # @return [Integer]
31
+ # See <https://redis.io/commands/exists> for more details.
32
+ # @parameter key [Key]
33
+ # @returns [Integer]
33
34
  def exists(key, *keys)
34
- call('EXISTS', key, *keys)
35
+ call("EXISTS", key, *keys)
35
36
  end
36
37
 
37
38
  # Boolean oversion of `exists`
38
- # @param key [Key]
39
- # @return [Boolean]
39
+ # @parameter key [Key]
40
+ # @returns [Boolean]
40
41
  def exists?(key, *keys)
41
42
  exists(key, *keys) > 0
42
43
  end
43
44
 
44
45
  # Set a key's time to live in seconds. O(1).
45
- # @see https://redis.io/commands/expire
46
- # @param key [Key]
47
- # @param seconds [Integer]
46
+ # See <https://redis.io/commands/expire> for more details.
47
+ # @parameter key [Key]
48
+ # @parameter seconds [Integer]
48
49
  def expire(key, seconds)
49
- call('EXPIRE', key, seconds)
50
+ call("EXPIRE", key, seconds)
50
51
  end
51
52
 
52
53
  # Set the expiration for a key as a UNIX timestamp. O(1).
53
- # @see https://redis.io/commands/expireat
54
- # @param key [Key]
55
- # @param timestamp [Posix time]
54
+ # See <https://redis.io/commands/expireat> for more details.
55
+ # @parameter key [Key]
56
+ # @parameter timestamp [Posix time]
56
57
  def expireat(key, time)
57
58
  case time
58
59
  when DateTime, Time, Date
59
- timestamp = time.strftime('%s').to_i
60
+ timestamp = time.strftime("%s").to_i
60
61
  else
61
62
  timestamp = time
62
63
  end
63
64
 
64
- call('EXPIREAT', key, timestamp)
65
+ call("EXPIREAT", key, timestamp)
65
66
  end
66
67
 
67
68
  # Find all keys matching the given pattern. O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
68
- # @see https://redis.io/commands/keys
69
- # @param pattern [Pattern]
69
+ # See <https://redis.io/commands/keys> for more details.
70
+ # @parameter pattern [Pattern]
70
71
  def keys(pattern)
71
- call('KEYS', pattern)
72
+ call("KEYS", pattern)
72
73
  end
73
74
 
74
75
  # Atomically transfer a key from a Redis instance to another one. This command actually executes a DUMP+DEL in the source instance, and a RESTORE in the target instance. See the pages of these commands for time complexity. Also an O(N) data transfer between the two instances is performed.
75
- # @see https://redis.io/commands/migrate
76
- # @param host [String]
77
- # @param port [String]
78
- # @param key [Enum]
79
- # @param destination-db [Integer]
80
- # @param timeout [Integer]
81
- # @param copy [Enum]
82
- # @param replace [Enum]
76
+ # See <https://redis.io/commands/migrate> for more details.
77
+ # @parameter host [String]
78
+ # @parameter port [String]
79
+ # @parameter key [Enum]
80
+ # @parameter destination-db [Integer]
81
+ # @parameter timeout [Integer]
82
+ # @parameter copy [Enum]
83
+ # @parameter replace [Enum]
83
84
  def migrate(host, port, destination = 0, keys:, timeout: 0, copy: false, replace: false, auth: nil)
84
85
  raise ArgumentError, "Must provide keys" if keys.empty?
85
86
 
@@ -113,94 +114,94 @@ module Protocol
113
114
  end
114
115
 
115
116
  # Move a key to another database. O(1).
116
- # @see https://redis.io/commands/move
117
- # @param key [Key]
118
- # @param db [Integer]
117
+ # See <https://redis.io/commands/move> for more details.
118
+ # @parameter key [Key]
119
+ # @parameter db [Integer]
119
120
  def move(key, db)
120
- call('MOVE', key, db)
121
+ call("MOVE", key, db)
121
122
  end
122
123
 
123
124
  # Inspect the internals of Redis objects. O(1) for all the currently implemented subcommands.
124
- # @see https://redis.io/commands/object
125
- # @param subcommand [String]
126
- # @param arguments [String]
125
+ # See <https://redis.io/commands/object> for more details.
126
+ # @parameter subcommand [String]
127
+ # @parameter arguments [String]
127
128
  def object(subcommand, *arguments)
128
- call('OBJECT', subcommand, *arguments)
129
+ call("OBJECT", subcommand, *arguments)
129
130
  end
130
131
 
131
132
  # Remove the expiration from a key. O(1).
132
- # @see https://redis.io/commands/persist
133
- # @param key [Key]
133
+ # See <https://redis.io/commands/persist> for more details.
134
+ # @parameter key [Key]
134
135
  def persist(key)
135
- call('PERSIST', key)
136
+ call("PERSIST", key)
136
137
  end
137
138
 
138
139
  # Set a key's time to live in milliseconds. O(1).
139
- # @see https://redis.io/commands/pexpire
140
- # @param key [Key]
141
- # @param milliseconds [Integer]
140
+ # See <https://redis.io/commands/pexpire> for more details.
141
+ # @parameter key [Key]
142
+ # @parameter milliseconds [Integer]
142
143
  def pexpire(key, milliseconds)
143
- call('PEXPIRE', milliseconds)
144
+ call("PEXPIRE", key, milliseconds)
144
145
  end
145
146
 
146
147
  # Set the expiration for a key as a UNIX timestamp specified in milliseconds. O(1).
147
- # @see https://redis.io/commands/pexpireat
148
- # @param key [Key]
149
- # @param milliseconds-timestamp [Posix time]
148
+ # See <https://redis.io/commands/pexpireat> for more details.
149
+ # @parameter key [Key]
150
+ # @parameter milliseconds-timestamp [Posix time]
150
151
  def pexpireat(key, time)
151
- case time.class
152
+ case time
152
153
  when DateTime, Time, Date
153
- timestamp = time.strftime('%Q').to_i
154
+ timestamp = (time.to_f * 1000).to_i
154
155
  else
155
156
  timestamp = time
156
157
  end
157
158
 
158
- call('PEXPIREAT', key, timestamp)
159
+ call("PEXPIREAT", key, timestamp)
159
160
  end
160
161
 
161
162
  # Get the time to live for a key in milliseconds. O(1).
162
- # @see https://redis.io/commands/pttl
163
- # @param key [Key]
163
+ # See <https://redis.io/commands/pttl> for more details.
164
+ # @parameter key [Key]
164
165
  def pttl(key)
165
- call('PTTL', key)
166
+ call("PTTL", key)
166
167
  end
167
168
 
168
169
  # Return a random key from the keyspace. O(1).
169
- # @see https://redis.io/commands/randomkey
170
+ # See <https://redis.io/commands/randomkey> for more details.
170
171
  def randomkey
171
- call('RANDOMKEY')
172
+ call("RANDOMKEY")
172
173
  end
173
174
 
174
175
  # Rename a key. O(1).
175
- # @see https://redis.io/commands/rename
176
- # @param key [Key]
177
- # @param newkey [Key]
176
+ # See <https://redis.io/commands/rename> for more details.
177
+ # @parameter key [Key]
178
+ # @parameter newkey [Key]
178
179
  def rename(key, new_key)
179
- call('RENAME', key, new_key)
180
+ call("RENAME", key, new_key)
180
181
  end
181
182
 
182
183
  # Rename a key, only if the new key does not exist. O(1).
183
- # @see https://redis.io/commands/renamenx
184
- # @param key [Key]
185
- # @param newkey [Key]
184
+ # See <https://redis.io/commands/renamenx> for more details.
185
+ # @parameter key [Key]
186
+ # @parameter newkey [Key]
186
187
  def renamenx(key, new_key)
187
- call('RENAMENX', key, new_key)
188
+ call("RENAMENX", key, new_key)
188
189
  end
189
190
 
190
191
  # Create a key using the provided serialized value, previously obtained using DUMP. O(1) to create the new key and additional O(N*M) to reconstruct the serialized value, where N is the number of Redis objects composing the value and M their average size. For small string values the time complexity is thus O(1)+O(1*M) where M is small, so simply O(1). However for sorted set values the complexity is O(N*M*log(N)) because inserting values into sorted sets is O(log(N)).
191
- # @see https://redis.io/commands/restore
192
- # @param key [Key]
193
- # @param ttl [Integer]
194
- # @param serialized-value [String]
195
- # @param replace [Enum]
196
- # @param absttl [Enum]
192
+ # See <https://redis.io/commands/restore> for more details.
193
+ # @parameter key [Key]
194
+ # @parameter ttl [Integer]
195
+ # @parameter serialized-value [String]
196
+ # @parameter replace [Enum]
197
+ # @parameter absttl [Enum]
197
198
  def restore(key, serialized_value, ttl=0)
198
- call('RESTORE', key, ttl, serialized_value)
199
+ call("RESTORE", key, ttl, serialized_value)
199
200
  end
200
201
 
201
202
  # Incrementally iterate the keys space. O(1) for every call. O(N) for a complete iteration, including enough command calls for the cursor to return back to 0. N is the number of elements inside the collection.
202
- # @see https://redis.io/commands/scan
203
- # @param cursor [Integer]
203
+ # See <https://redis.io/commands/scan> for more details.
204
+ # @parameter cursor [Integer]
204
205
  def scan(cursor, match: nil, count: nil, type: nil)
205
206
  arguments = [cursor]
206
207
 
@@ -220,11 +221,11 @@ module Protocol
220
221
  end
221
222
 
222
223
  # Sort the elements in a list, set or sorted set. O(N+M*log(M)) where N is the number of elements in the list or set to sort, and M the number of returned elements. When the elements are not sorted, complexity is currently O(N) as there is a copy step that will be avoided in next releases.
223
- # @see https://redis.io/commands/sort
224
- # @param key [Key]
225
- # @param order [Enum]
226
- # @param sorting [Enum]
227
- def sort(key, by: nil, offset: nil, count: nil, get: nil, order: 'ASC', alpha: false, store: nil)
224
+ # See <https://redis.io/commands/sort> for more details.
225
+ # @parameter key [Key]
226
+ # @parameter order [Enum]
227
+ # @parameter sorting [Enum]
228
+ def sort(key, by: nil, offset: nil, count: nil, get: nil, order: "ASC", alpha: false, store: nil)
228
229
  arguments = []
229
230
 
230
231
  if by
@@ -251,42 +252,42 @@ module Protocol
251
252
  arguments.append("STORE", store)
252
253
  end
253
254
 
254
- call('SORT', *arguments)
255
+ call("SORT", key, *arguments)
255
256
  end
256
257
 
257
258
  # Alters the last access time of a key(s). Returns the number of existing keys specified. O(N) where N is the number of keys that will be touched.
258
- # @see https://redis.io/commands/touch
259
- # @param key [Key]
259
+ # See <https://redis.io/commands/touch> for more details.
260
+ # @parameter key [Key]
260
261
  def touch(key, *keys)
261
- call('TOUCH', key, *keys)
262
+ call("TOUCH", key, *keys)
262
263
  end
263
264
 
264
265
  # Get the time to live for a key. O(1).
265
- # @see https://redis.io/commands/ttl
266
- # @param key [Key]
266
+ # See <https://redis.io/commands/ttl> for more details.
267
+ # @parameter key [Key]
267
268
  def ttl(key)
268
- call('TTL', key)
269
+ call("TTL", key)
269
270
  end
270
271
 
271
272
  # Determine the type stored at key. O(1).
272
- # @see https://redis.io/commands/type
273
- # @param key [Key]
273
+ # See <https://redis.io/commands/type> for more details.
274
+ # @parameter key [Key]
274
275
  def type(key)
275
- call('TYPE', key)
276
+ call("TYPE", key)
276
277
  end
277
278
 
278
279
  # Delete a key asynchronously in another thread. Otherwise it is just as DEL, but non blocking. O(1) for each key removed regardless of its size. Then the command does O(N) work in a different thread in order to reclaim memory, where N is the number of allocations the deleted objects where composed of.
279
- # @see https://redis.io/commands/unlink
280
- # @param key [Key]
280
+ # See <https://redis.io/commands/unlink> for more details.
281
+ # @parameter key [Key]
281
282
  def unlink(key)
282
- call('UNLINK', key)
283
+ call("UNLINK", key)
283
284
  end
284
-
285
+
285
286
  # Wait for the synchronous replication of all the write commands sent in the context of the current connection. O(1).
286
- # @see https://redis.io/commands/wait
287
- # @param numreplicas [Integer]
288
- # @param timeout [Integer]
289
- def wait(newreplicas, timeout = 0)
287
+ # See <https://redis.io/commands/wait> for more details.
288
+ # @parameter numreplicas [Integer]
289
+ # @parameter timeout [Integer]
290
+ def wait(numreplicas, timeout = 0)
290
291
  call("WAIT", numreplicas, timeout)
291
292
  end
292
293
  end
@@ -6,54 +6,55 @@
6
6
  module Protocol
7
7
  module Redis
8
8
  module Methods
9
+ # Methods for managing Redis geospatial indexes.
9
10
  module Geospatial
10
11
  # Add one or more geospatial items in the geospatial index represented using a sorted set. O(log(N)) for each item added, where N is the number of elements in the sorted set.
11
- # @see https://redis.io/commands/geoadd
12
- # @param key [Key]
12
+ # See <https://redis.io/commands/geoadd> for more details.
13
+ # @parameter key [Key]
13
14
  def geoadd(key, longitude, latitude, member, *arguments)
14
- call("GEOADD", longitude, latitude, member, *arguments)
15
+ call("GEOADD", key, longitude, latitude, member, *arguments)
15
16
  end
16
17
 
17
18
  # Returns members of a geospatial index as standard geohash strings. O(log(N)) for each member requested, where N is the number of elements in the sorted set.
18
- # @see https://redis.io/commands/geohash
19
- # @param key [Key]
20
- # @param member [String]
19
+ # See <https://redis.io/commands/geohash> for more details.
20
+ # @parameter key [Key]
21
+ # @parameter member [String]
21
22
  def geohash(key, member, *members)
22
23
  call("GEOHASH", key, member, *members)
23
24
  end
24
25
 
25
26
  # Returns longitude and latitude of members of a geospatial index. O(log(N)) for each member requested, where N is the number of elements in the sorted set.
26
- # @see https://redis.io/commands/geopos
27
- # @param key [Key]
28
- # @param member [String]
27
+ # See <https://redis.io/commands/geopos> for more details.
28
+ # @parameter key [Key]
29
+ # @parameter member [String]
29
30
  def geopos(key, member, *members)
30
31
  call("GEOPOS", key, member, *members)
31
32
  end
32
33
 
33
34
  # Returns the distance between two members of a geospatial index. O(log(N)).
34
- # @see https://redis.io/commands/geodist
35
- # @param key [Key]
36
- # @param member1 [String]
37
- # @param member2 [String]
38
- # @param unit [Enum] Distance scale to use, one of "m" (meters), "km" (kilometers), "mi" (miles) or "ft" (feet).
35
+ # See <https://redis.io/commands/geodist> for more details.
36
+ # @parameter key [Key]
37
+ # @parameter member1 [String]
38
+ # @parameter member2 [String]
39
+ # @parameter unit [Enum] Distance scale to use, one of "m" (meters), "km" (kilometers), "mi" (miles) or "ft" (feet).
39
40
  def geodist(key, from, to, unit = "m")
40
41
  call("GEODIST", key, from, to, unit)
41
42
  end
42
43
 
43
44
  # Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point. O(N+log(M)) where N is the number of elements inside the bounding box of the circular area delimited by center and radius and M is the number of items inside the index.
44
- # @see https://redis.io/commands/georadius
45
- # @param key [Key]
46
- # @param longitude [Double]
47
- # @param latitude [Double]
48
- # @param radius [Double]
49
- # @param unit [Enum]
50
- # @param count [Integer] Limit the number of results to at most this many.
51
- # @param order [Symbol] `:ASC` Sort returned items from the nearest to the farthest, relative to the center. `:DESC` Sort returned items from the farthest to the nearest, relative to the center.
52
- # @param with_coordinates [Boolean] Also return the longitude,latitude coordinates of the matching items.
53
- # @param with_distance [Boolean] Also return the distance of the returned items from the specified center. The distance is returned in the same unit as the unit specified as the radius argument of the command.
54
- # @param with_hash [Boolean] Also return the raw geohash-encoded sorted set score of the item, in the form of a 52 bit unsigned integer. This is only useful for low level hacks or debugging and is otherwise of little interest for the general user.
55
- # @param store [Key]
56
- # @param store_distance [Key]
45
+ # See <https://redis.io/commands/georadius> for more details.
46
+ # @parameter key [Key]
47
+ # @parameter longitude [Double]
48
+ # @parameter latitude [Double]
49
+ # @parameter radius [Double]
50
+ # @parameter unit [Enum]
51
+ # @parameter count [Integer] Limit the number of results to at most this many.
52
+ # @parameter order [Symbol] `:ASC` Sort returned items from the nearest to the farthest, relative to the center. `:DESC` Sort returned items from the farthest to the nearest, relative to the center.
53
+ # @parameter with_coordinates [Boolean] Also return the longitude,latitude coordinates of the matching items.
54
+ # @parameter with_distance [Boolean] Also return the distance of the returned items from the specified center. The distance is returned in the same unit as the unit specified as the radius argument of the command.
55
+ # @parameter with_hash [Boolean] Also return the raw geohash-encoded sorted set score of the item, in the form of a 52 bit unsigned integer. This is only useful for low level hacks or debugging and is otherwise of little interest for the general user.
56
+ # @parameter store [Key]
57
+ # @parameter store_distance [Key]
57
58
  def georadius(key, longitude, latitude, radius, unit = "m", with_coordinates: false, with_distance: false, with_hash: false, count: nil, order: nil, store: nil, store_distance: nil)
58
59
  arguments = [key, longitude, latitude, radius, unit]
59
60
 
@@ -85,7 +86,7 @@ module Protocol
85
86
  end
86
87
 
87
88
  if store_distance
88
- arguments.append("STOREDIST", storedist)
89
+ arguments.append("STOREDIST", store_distance)
89
90
  readonly = false
90
91
  end
91
92
 
@@ -98,18 +99,18 @@ module Protocol
98
99
  end
99
100
 
100
101
  # Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member. O(N+log(M)) where N is the number of elements inside the bounding box of the circular area delimited by center and radius and M is the number of items inside the index.
101
- # @see https://redis.io/commands/georadiusbymember
102
- # @param key [Key]
103
- # @param member [String]
104
- # @param radius [Double]
105
- # @param unit [Enum]
106
- # @param count [Integer] Limit the number of results to at most this many.
107
- # @param order [Symbol] `:ASC` Sort returned items from the nearest to the farthest, relative to the center. `:DESC` Sort returned items from the farthest to the nearest, relative to the center.
108
- # @param with_coordinates [Boolean] Also return the longitude,latitude coordinates of the matching items.
109
- # @param with_distance [Boolean] Also return the distance of the returned items from the specified center. The distance is returned in the same unit as the unit specified as the radius argument of the command.
110
- # @param with_hash [Boolean] Also return the raw geohash-encoded sorted set score of the item, in the form of a 52 bit unsigned integer. This is only useful for low level hacks or debugging and is otherwise of little interest for the general user.
111
- # @param store [Key]
112
- # @param store_distance [Key]
102
+ # See <https://redis.io/commands/georadiusbymember> for more details.
103
+ # @parameter key [Key]
104
+ # @parameter member [String]
105
+ # @parameter radius [Double]
106
+ # @parameter unit [Enum]
107
+ # @parameter count [Integer] Limit the number of results to at most this many.
108
+ # @parameter order [Symbol] `:ASC` Sort returned items from the nearest to the farthest, relative to the center. `:DESC` Sort returned items from the farthest to the nearest, relative to the center.
109
+ # @parameter with_coordinates [Boolean] Also return the longitude,latitude coordinates of the matching items.
110
+ # @parameter with_distance [Boolean] Also return the distance of the returned items from the specified center. The distance is returned in the same unit as the unit specified as the radius argument of the command.
111
+ # @parameter with_hash [Boolean] Also return the raw geohash-encoded sorted set score of the item, in the form of a 52 bit unsigned integer. This is only useful for low level hacks or debugging and is otherwise of little interest for the general user.
112
+ # @parameter store [Key]
113
+ # @parameter store_distance [Key]
113
114
  def georadiusbymember(key, member, radius, unit = "m", with_coordinates: false, with_distance: false, with_hash: false, count: nil, order: nil, store: nil, store_distance: nil)
114
115
  arguments = [key, member, radius, unit]
115
116
 
@@ -133,14 +134,6 @@ module Protocol
133
134
  arguments.append(order)
134
135
  end
135
136
 
136
- if store
137
- arguments.append("STORE", store)
138
- end
139
-
140
- if store_distance
141
- arguments.append("STOREDIST", storedist)
142
- end
143
-
144
137
  readonly = true
145
138
 
146
139
  if store
@@ -149,7 +142,7 @@ module Protocol
149
142
  end
150
143
 
151
144
  if store_distance
152
- arguments.append("STOREDIST", storedist)
145
+ arguments.append("STOREDIST", store_distance)
153
146
  readonly = false
154
147
  end
155
148