ultra-smart-kit 0.0.1
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.
- checksums.yaml +7 -0
- data/redis-5.4.1/CHANGELOG.md +693 -0
- data/redis-5.4.1/LICENSE +20 -0
- data/redis-5.4.1/README.md +453 -0
- data/redis-5.4.1/lib/redis/client.rb +131 -0
- data/redis-5.4.1/lib/redis/commands/bitmaps.rb +73 -0
- data/redis-5.4.1/lib/redis/commands/cluster.rb +28 -0
- data/redis-5.4.1/lib/redis/commands/connection.rb +53 -0
- data/redis-5.4.1/lib/redis/commands/geo.rb +84 -0
- data/redis-5.4.1/lib/redis/commands/hashes.rb +258 -0
- data/redis-5.4.1/lib/redis/commands/hyper_log_log.rb +37 -0
- data/redis-5.4.1/lib/redis/commands/keys.rb +459 -0
- data/redis-5.4.1/lib/redis/commands/lists.rb +339 -0
- data/redis-5.4.1/lib/redis/commands/pubsub.rb +81 -0
- data/redis-5.4.1/lib/redis/commands/scripting.rb +114 -0
- data/redis-5.4.1/lib/redis/commands/server.rb +188 -0
- data/redis-5.4.1/lib/redis/commands/sets.rb +218 -0
- data/redis-5.4.1/lib/redis/commands/sorted_sets.rb +922 -0
- data/redis-5.4.1/lib/redis/commands/streams.rb +409 -0
- data/redis-5.4.1/lib/redis/commands/strings.rb +314 -0
- data/redis-5.4.1/lib/redis/commands/transactions.rb +115 -0
- data/redis-5.4.1/lib/redis/commands.rb +239 -0
- data/redis-5.4.1/lib/redis/distributed.rb +1107 -0
- data/redis-5.4.1/lib/redis/errors.rb +67 -0
- data/redis-5.4.1/lib/redis/hash_ring.rb +89 -0
- data/redis-5.4.1/lib/redis/pipeline.rb +131 -0
- data/redis-5.4.1/lib/redis/subscribe.rb +126 -0
- data/redis-5.4.1/lib/redis/version.rb +5 -0
- data/redis-5.4.1/lib/redis.rb +196 -0
- data/ultra-smart-kit.gemspec +12 -0
- metadata +70 -0
data/redis-5.4.1/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 Ezra Zygmuntowicz
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
# redis-rb [![Build Status][gh-actions-image]][gh-actions-link] [![Inline docs][rdoc-master-image]][rdoc-master-link]
|
|
2
|
+
|
|
3
|
+
A Ruby client that tries to match [Redis][redis-home]' API one-to-one, while still providing an idiomatic interface.
|
|
4
|
+
|
|
5
|
+
See [RubyDoc.info][rubydoc] for the API docs of the latest published gem.
|
|
6
|
+
|
|
7
|
+
## Getting started
|
|
8
|
+
|
|
9
|
+
Install with:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
$ gem install redis
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
You can connect to Redis by instantiating the `Redis` class:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
require "redis"
|
|
19
|
+
|
|
20
|
+
redis = Redis.new
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
This assumes Redis was started with a default configuration, and is
|
|
24
|
+
listening on `localhost`, port 6379. If you need to connect to a remote
|
|
25
|
+
server or a different port, try:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
redis = Redis.new(host: "10.0.1.1", port: 6380, db: 15)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
You can also specify connection options as a [`redis://` URL][redis-url]:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
redis = Redis.new(url: "redis://:p4ssw0rd@10.0.1.1:6380/15")
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The client expects passwords with special characters to be URL-encoded (i.e.
|
|
38
|
+
`CGI.escape(password)`).
|
|
39
|
+
|
|
40
|
+
To connect to Redis listening on a Unix socket, try:
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
redis = Redis.new(path: "/tmp/redis.sock")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
To connect to a password protected Redis instance, use:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
redis = Redis.new(password: "mysecret")
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
To connect a Redis instance using [ACL](https://redis.io/topics/acl), use:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
redis = Redis.new(username: 'myname', password: 'mysecret')
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The Redis class exports methods that are named identical to the commands
|
|
59
|
+
they execute. The arguments these methods accept are often identical to
|
|
60
|
+
the arguments specified on the [Redis website][redis-commands]. For
|
|
61
|
+
instance, the `SET` and `GET` commands can be called like this:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
redis.set("mykey", "hello world")
|
|
65
|
+
# => "OK"
|
|
66
|
+
|
|
67
|
+
redis.get("mykey")
|
|
68
|
+
# => "hello world"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
All commands, their arguments, and return values are documented and
|
|
72
|
+
available on [RubyDoc.info][rubydoc].
|
|
73
|
+
|
|
74
|
+
## Connection Pooling and Thread safety
|
|
75
|
+
|
|
76
|
+
The client does not provide connection pooling. Each `Redis` instance
|
|
77
|
+
has one and only one connection to the server, and use of this connection
|
|
78
|
+
is protected by a mutex.
|
|
79
|
+
|
|
80
|
+
As such it is heavily recommended to use the [`connection_pool` gem](https://github.com/mperham/connection_pool), e.g.:
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
module MyApp
|
|
84
|
+
def self.redis
|
|
85
|
+
@redis ||= ConnectionPool::Wrapper.new do
|
|
86
|
+
Redis.new(url: ENV["REDIS_URL"])
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
MyApp.redis.incr("some-counter")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Sentinel support
|
|
95
|
+
|
|
96
|
+
The client is able to perform automatic failover by using [Redis
|
|
97
|
+
Sentinel](http://redis.io/topics/sentinel). Make sure to run Redis 2.8+
|
|
98
|
+
if you want to use this feature.
|
|
99
|
+
|
|
100
|
+
To connect using Sentinel, use:
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
SENTINELS = [{ host: "127.0.0.1", port: 26380 },
|
|
104
|
+
{ host: "127.0.0.1", port: 26381 }]
|
|
105
|
+
|
|
106
|
+
redis = Redis.new(name: "mymaster", sentinels: SENTINELS, role: :master)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
* The master name identifies a group of Redis instances composed of a master
|
|
110
|
+
and one or more slaves (`mymaster` in the example).
|
|
111
|
+
|
|
112
|
+
* It is possible to optionally provide a role. The allowed roles are `master`
|
|
113
|
+
and `slave`. When the role is `slave`, the client will try to connect to a
|
|
114
|
+
random slave of the specified master. If a role is not specified, the client
|
|
115
|
+
will connect to the master.
|
|
116
|
+
|
|
117
|
+
* When using the Sentinel support you need to specify a list of sentinels to
|
|
118
|
+
connect to. The list does not need to enumerate all your Sentinel instances,
|
|
119
|
+
but a few so that if one is down the client will try the next one. The client
|
|
120
|
+
is able to remember the last Sentinel that was able to reply correctly and will
|
|
121
|
+
use it for the next requests.
|
|
122
|
+
|
|
123
|
+
To [authenticate](https://redis.io/docs/management/sentinel/#configuring-sentinel-instances-with-authentication) Sentinel itself, you can specify the `sentinel_username` and `sentinel_password`. Exclude the `sentinel_username` option if you're using password-only authentication.
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
SENTINELS = [{ host: '127.0.0.1', port: 26380},
|
|
127
|
+
{ host: '127.0.0.1', port: 26381}]
|
|
128
|
+
|
|
129
|
+
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, sentinel_username: 'appuser', sentinel_password: 'mysecret', role: :master)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
If you specify a username and/or password at the top level for your main Redis instance, Sentinel *will not* using thouse credentials
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
# Use 'mysecret' to authenticate against the mymaster instance, but skip authentication for the sentinels:
|
|
136
|
+
SENTINELS = [{ host: '127.0.0.1', port: 26380 },
|
|
137
|
+
{ host: '127.0.0.1', port: 26381 }]
|
|
138
|
+
|
|
139
|
+
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, role: :master, password: 'mysecret')
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
So you have to provide Sentinel credential and Redis explicitly even they are the same
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
# Use 'mysecret' to authenticate against the mymaster instance and sentinel
|
|
146
|
+
SENTINELS = [{ host: '127.0.0.1', port: 26380 },
|
|
147
|
+
{ host: '127.0.0.1', port: 26381 }]
|
|
148
|
+
|
|
149
|
+
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, role: :master, password: 'mysecret', sentinel_password: 'mysecret')
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Also the `name`, `password`, `username` and `db` for Redis instance can be passed as an url:
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
redis = Redis.new(url: "redis://appuser:mysecret@mymaster/10", sentinels: SENTINELS, role: :master)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Cluster support
|
|
159
|
+
|
|
160
|
+
[Clustering](https://redis.io/topics/cluster-spec). is supported via the [`redis-clustering` gem](cluster/).
|
|
161
|
+
|
|
162
|
+
## Pipelining
|
|
163
|
+
|
|
164
|
+
When multiple commands are executed sequentially, but are not dependent,
|
|
165
|
+
the calls can be *pipelined*. This means that the client doesn't wait
|
|
166
|
+
for reply of the first command before sending the next command. The
|
|
167
|
+
advantage is that multiple commands are sent at once, resulting in
|
|
168
|
+
faster overall execution.
|
|
169
|
+
|
|
170
|
+
The client can be instructed to pipeline commands by using the
|
|
171
|
+
`#pipelined` method. After the block is executed, the client sends all
|
|
172
|
+
commands to Redis and gathers their replies. These replies are returned
|
|
173
|
+
by the `#pipelined` method.
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
redis.pipelined do |pipeline|
|
|
177
|
+
pipeline.set "foo", "bar"
|
|
178
|
+
pipeline.incr "baz"
|
|
179
|
+
end
|
|
180
|
+
# => ["OK", 1]
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Commands must be called on the yielded objects. If you call methods
|
|
184
|
+
on the original client objects from inside a pipeline, they will be sent immediately:
|
|
185
|
+
|
|
186
|
+
```ruby
|
|
187
|
+
redis.pipelined do |pipeline|
|
|
188
|
+
pipeline.set "foo", "bar"
|
|
189
|
+
redis.incr "baz" # => 1
|
|
190
|
+
end
|
|
191
|
+
# => ["OK"]
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Exception management
|
|
195
|
+
|
|
196
|
+
The `exception` flag in the `#pipelined` is a feature that modifies the pipeline execution behavior. When set
|
|
197
|
+
to `false`, it doesn't raise an exception when a command error occurs. Instead, it allows the pipeline to execute all
|
|
198
|
+
commands, and any failed command will be available in the returned array. (Defaults to `true`)
|
|
199
|
+
|
|
200
|
+
```ruby
|
|
201
|
+
results = redis.pipelined(exception: false) do |pipeline|
|
|
202
|
+
pipeline.set('key1', 'value1')
|
|
203
|
+
pipeline.lpush('key1', 'something') # This will fail
|
|
204
|
+
pipeline.set('key2', 'value2')
|
|
205
|
+
end
|
|
206
|
+
# results => ["OK", #<RedisClient::WrongTypeError: WRONGTYPE Operation against a key holding the wrong kind of value>, "OK"]
|
|
207
|
+
|
|
208
|
+
results.each do |result|
|
|
209
|
+
if result.is_a?(Redis::CommandError)
|
|
210
|
+
# Do something with the failed result
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
### Executing commands atomically
|
|
217
|
+
|
|
218
|
+
You can use `MULTI/EXEC` to run a number of commands in an atomic
|
|
219
|
+
fashion. This is similar to executing a pipeline, but the commands are
|
|
220
|
+
preceded by a call to `MULTI`, and followed by a call to `EXEC`. Like
|
|
221
|
+
the regular pipeline, the replies to the commands are returned by the
|
|
222
|
+
`#multi` method.
|
|
223
|
+
|
|
224
|
+
```ruby
|
|
225
|
+
redis.multi do |transaction|
|
|
226
|
+
transaction.set "foo", "bar"
|
|
227
|
+
transaction.incr "baz"
|
|
228
|
+
end
|
|
229
|
+
# => ["OK", 1]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Futures
|
|
233
|
+
|
|
234
|
+
Replies to commands in a pipeline can be accessed via the *futures* they
|
|
235
|
+
emit. All calls on the pipeline object return a
|
|
236
|
+
`Future` object, which responds to the `#value` method. When the
|
|
237
|
+
pipeline has successfully executed, all futures are assigned their
|
|
238
|
+
respective replies and can be used.
|
|
239
|
+
|
|
240
|
+
```ruby
|
|
241
|
+
set = incr = nil
|
|
242
|
+
redis.pipelined do |pipeline|
|
|
243
|
+
set = pipeline.set "foo", "bar"
|
|
244
|
+
incr = pipeline.incr "baz"
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
set.value
|
|
248
|
+
# => "OK"
|
|
249
|
+
|
|
250
|
+
incr.value
|
|
251
|
+
# => 1
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Error Handling
|
|
255
|
+
|
|
256
|
+
In general, if something goes wrong you'll get an exception. For example, if
|
|
257
|
+
it can't connect to the server a `Redis::CannotConnectError` error will be raised.
|
|
258
|
+
|
|
259
|
+
```ruby
|
|
260
|
+
begin
|
|
261
|
+
redis.ping
|
|
262
|
+
rescue Redis::BaseError => e
|
|
263
|
+
e.inspect
|
|
264
|
+
# => #<Redis::CannotConnectError: Timed out connecting to Redis on 10.0.1.1:6380>
|
|
265
|
+
|
|
266
|
+
e.message
|
|
267
|
+
# => Timed out connecting to Redis on 10.0.1.1:6380
|
|
268
|
+
end
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
See lib/redis/errors.rb for information about what exceptions are possible.
|
|
272
|
+
|
|
273
|
+
## Timeouts
|
|
274
|
+
|
|
275
|
+
The client allows you to configure connect, read, and write timeouts.
|
|
276
|
+
Starting in version 5.0, the default for each is 1. Before that, it was 5.
|
|
277
|
+
Passing a single `timeout` option will set all three values:
|
|
278
|
+
|
|
279
|
+
```ruby
|
|
280
|
+
Redis.new(:timeout => 1)
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
But you can use specific values for each of them:
|
|
284
|
+
|
|
285
|
+
```ruby
|
|
286
|
+
Redis.new(
|
|
287
|
+
:connect_timeout => 0.2,
|
|
288
|
+
:read_timeout => 1.0,
|
|
289
|
+
:write_timeout => 0.5
|
|
290
|
+
)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
All timeout values are specified in seconds.
|
|
294
|
+
|
|
295
|
+
When using pub/sub, you can subscribe to a channel using a timeout as well:
|
|
296
|
+
|
|
297
|
+
```ruby
|
|
298
|
+
redis = Redis.new(reconnect_attempts: 0)
|
|
299
|
+
redis.subscribe_with_timeout(5, "news") do |on|
|
|
300
|
+
on.message do |channel, message|
|
|
301
|
+
# ...
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
If no message is received after 5 seconds, the client will unsubscribe.
|
|
307
|
+
|
|
308
|
+
## Reconnections
|
|
309
|
+
|
|
310
|
+
**By default**, this gem will only **retry a connection once** and then fail, but
|
|
311
|
+
the client allows you to configure how many `reconnect_attempts` it should
|
|
312
|
+
complete before declaring a connection as failed.
|
|
313
|
+
|
|
314
|
+
```ruby
|
|
315
|
+
Redis.new(reconnect_attempts: 0)
|
|
316
|
+
Redis.new(reconnect_attempts: 3)
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
If you wish to wait between reconnection attempts, you can instead pass a list
|
|
320
|
+
of durations:
|
|
321
|
+
|
|
322
|
+
```ruby
|
|
323
|
+
Redis.new(reconnect_attempts: [
|
|
324
|
+
0, # retry immediately
|
|
325
|
+
0.25, # retry a second time after 250ms
|
|
326
|
+
1, # retry a third and final time after another 1s
|
|
327
|
+
])
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
If you wish to disable reconnection only for some commands, you can use
|
|
331
|
+
`disable_reconnection`:
|
|
332
|
+
|
|
333
|
+
```ruby
|
|
334
|
+
redis.get("some-key") # this may be retried
|
|
335
|
+
redis.disable_reconnection do
|
|
336
|
+
redis.incr("some-counter") # this won't be retried.
|
|
337
|
+
end
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## SSL/TLS Support
|
|
341
|
+
|
|
342
|
+
To enable SSL support, pass the `:ssl => true` option when configuring the
|
|
343
|
+
Redis client, or pass in `:url => "rediss://..."` (like HTTPS for Redis).
|
|
344
|
+
You will also need to pass in an `:ssl_params => { ... }` hash used to
|
|
345
|
+
configure the `OpenSSL::SSL::SSLContext` object used for the connection:
|
|
346
|
+
|
|
347
|
+
```ruby
|
|
348
|
+
redis = Redis.new(
|
|
349
|
+
:url => "rediss://:p4ssw0rd@10.0.1.1:6381/15",
|
|
350
|
+
:ssl_params => {
|
|
351
|
+
:ca_file => "/path/to/ca.crt"
|
|
352
|
+
}
|
|
353
|
+
)
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
The options given to `:ssl_params` are passed directly to the
|
|
357
|
+
`OpenSSL::SSL::SSLContext#set_params` method and can be any valid attribute
|
|
358
|
+
of the SSL context. Please see the [OpenSSL::SSL::SSLContext documentation]
|
|
359
|
+
for all of the available attributes.
|
|
360
|
+
|
|
361
|
+
Here is an example of passing in params that can be used for SSL client
|
|
362
|
+
certificate authentication (a.k.a. mutual TLS):
|
|
363
|
+
|
|
364
|
+
```ruby
|
|
365
|
+
redis = Redis.new(
|
|
366
|
+
:url => "rediss://:p4ssw0rd@10.0.1.1:6381/15",
|
|
367
|
+
:ssl_params => {
|
|
368
|
+
:ca_file => "/path/to/ca.crt",
|
|
369
|
+
:cert => OpenSSL::X509::Certificate.new(File.read("client.crt")),
|
|
370
|
+
:key => OpenSSL::PKey::RSA.new(File.read("client.key"))
|
|
371
|
+
}
|
|
372
|
+
)
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
[OpenSSL::SSL::SSLContext documentation]: http://ruby-doc.org/stdlib-2.5.0/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html
|
|
376
|
+
|
|
377
|
+
## Expert-Mode Options
|
|
378
|
+
|
|
379
|
+
- `inherit_socket: true`: disable safety check that prevents a forked child
|
|
380
|
+
from sharing a socket with its parent; this is potentially useful in order to mitigate connection churn when:
|
|
381
|
+
- many short-lived forked children of one process need to talk
|
|
382
|
+
to redis, AND
|
|
383
|
+
- your own code prevents the parent process from using the redis
|
|
384
|
+
connection while a child is alive
|
|
385
|
+
|
|
386
|
+
Improper use of `inherit_socket` will result in corrupted and/or incorrect
|
|
387
|
+
responses.
|
|
388
|
+
|
|
389
|
+
## hiredis binding
|
|
390
|
+
|
|
391
|
+
By default, redis-rb uses Ruby's socket library to talk with Redis.
|
|
392
|
+
|
|
393
|
+
The hiredis driver uses the connection facility of hiredis-rb. In turn,
|
|
394
|
+
hiredis-rb is a binding to the official hiredis client library. It
|
|
395
|
+
optimizes for speed, at the cost of portability. Because it is a C
|
|
396
|
+
extension, JRuby is not supported (by default).
|
|
397
|
+
|
|
398
|
+
It is best to use hiredis when you have large replies (for example:
|
|
399
|
+
`LRANGE`, `SMEMBERS`, `ZRANGE`, etc.) and/or use big pipelines.
|
|
400
|
+
|
|
401
|
+
In your Gemfile, include `hiredis-client`:
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
gem "redis"
|
|
405
|
+
gem "hiredis-client"
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
If your application doesn't call `Bundler.require`, you may have
|
|
409
|
+
to require it explicitly:
|
|
410
|
+
|
|
411
|
+
```ruby
|
|
412
|
+
require "hiredis-client"
|
|
413
|
+
````
|
|
414
|
+
|
|
415
|
+
This makes the hiredis driver the default.
|
|
416
|
+
|
|
417
|
+
If you want to be certain hiredis is being used, when instantiating
|
|
418
|
+
the client object, specify hiredis:
|
|
419
|
+
|
|
420
|
+
```ruby
|
|
421
|
+
redis = Redis.new(driver: :hiredis)
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
## Testing
|
|
425
|
+
|
|
426
|
+
This library is tested against recent Ruby and Redis versions.
|
|
427
|
+
Check [Github Actions][gh-actions-link] for the exact versions supported.
|
|
428
|
+
|
|
429
|
+
## See Also
|
|
430
|
+
|
|
431
|
+
- [async-redis](https://github.com/socketry/async-redis) — An [async](https://github.com/socketry/async) compatible Redis client.
|
|
432
|
+
|
|
433
|
+
## Contributors
|
|
434
|
+
|
|
435
|
+
Several people contributed to redis-rb, but we would like to especially
|
|
436
|
+
mention Ezra Zygmuntowicz. Ezra introduced the Ruby community to many
|
|
437
|
+
new cool technologies, like Redis. He wrote the first version of this
|
|
438
|
+
client and evangelized Redis in Rubyland. Thank you, Ezra.
|
|
439
|
+
|
|
440
|
+
## Contributing
|
|
441
|
+
|
|
442
|
+
[Fork the project](https://github.com/redis/redis-rb) and send pull
|
|
443
|
+
requests.
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
[rdoc-master-image]: https://img.shields.io/badge/docs-rdoc.info-blue.svg
|
|
447
|
+
[rdoc-master-link]: https://rubydoc.info/github/redis/redis-rb
|
|
448
|
+
[redis-commands]: https://redis.io/commands
|
|
449
|
+
[redis-home]: https://redis.io
|
|
450
|
+
[redis-url]: https://www.iana.org/assignments/uri-schemes/prov/redis
|
|
451
|
+
[gh-actions-image]: https://github.com/redis/redis-rb/workflows/Test/badge.svg
|
|
452
|
+
[gh-actions-link]: https://github.com/redis/redis-rb/actions
|
|
453
|
+
[rubydoc]: https://rubydoc.info/gems/redis
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
class Client < ::RedisClient
|
|
5
|
+
ERROR_MAPPING = {
|
|
6
|
+
RedisClient::ConnectionError => Redis::ConnectionError,
|
|
7
|
+
RedisClient::CommandError => Redis::CommandError,
|
|
8
|
+
RedisClient::ReadTimeoutError => Redis::TimeoutError,
|
|
9
|
+
RedisClient::CannotConnectError => Redis::CannotConnectError,
|
|
10
|
+
RedisClient::AuthenticationError => Redis::CannotConnectError,
|
|
11
|
+
RedisClient::FailoverError => Redis::CannotConnectError,
|
|
12
|
+
RedisClient::PermissionError => Redis::PermissionError,
|
|
13
|
+
RedisClient::WrongTypeError => Redis::WrongTypeError,
|
|
14
|
+
RedisClient::ReadOnlyError => Redis::ReadOnlyError,
|
|
15
|
+
RedisClient::ProtocolError => Redis::ProtocolError,
|
|
16
|
+
RedisClient::OutOfMemoryError => Redis::OutOfMemoryError,
|
|
17
|
+
}
|
|
18
|
+
if defined?(RedisClient::NoScriptError)
|
|
19
|
+
ERROR_MAPPING[RedisClient::NoScriptError] = Redis::NoScriptError
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
def config(**kwargs)
|
|
24
|
+
super(protocol: 2, **kwargs)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def sentinel(**kwargs)
|
|
28
|
+
super(protocol: 2, **kwargs, client_implementation: ::RedisClient)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def translate_error!(error, mapping: ERROR_MAPPING)
|
|
32
|
+
redis_error = translate_error_class(error.class, mapping: mapping)
|
|
33
|
+
raise redis_error, error.message, error.backtrace
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def translate_error_class(error_class, mapping: ERROR_MAPPING)
|
|
39
|
+
mapping.fetch(error_class)
|
|
40
|
+
rescue IndexError
|
|
41
|
+
if (client_error = error_class.ancestors.find { |a| mapping[a] })
|
|
42
|
+
mapping[error_class] = mapping[client_error]
|
|
43
|
+
else
|
|
44
|
+
raise
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def id
|
|
50
|
+
config.id
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def server_url
|
|
54
|
+
config.server_url
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def timeout
|
|
58
|
+
config.read_timeout
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def db
|
|
62
|
+
config.db
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def host
|
|
66
|
+
config.host unless config.path
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def port
|
|
70
|
+
config.port unless config.path
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def path
|
|
74
|
+
config.path
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def username
|
|
78
|
+
config.username
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def password
|
|
82
|
+
config.password
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
undef_method :call
|
|
86
|
+
undef_method :call_once
|
|
87
|
+
undef_method :call_once_v
|
|
88
|
+
undef_method :blocking_call
|
|
89
|
+
|
|
90
|
+
def ensure_connected(retryable: true, &block)
|
|
91
|
+
super(retryable: retryable, &block)
|
|
92
|
+
rescue ::RedisClient::Error => error
|
|
93
|
+
Client.translate_error!(error)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def call_v(command, &block)
|
|
97
|
+
super(command, &block)
|
|
98
|
+
rescue ::RedisClient::Error => error
|
|
99
|
+
Client.translate_error!(error)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def blocking_call_v(timeout, command, &block)
|
|
103
|
+
if timeout && timeout > 0
|
|
104
|
+
# Can't use the command timeout argument as the connection timeout
|
|
105
|
+
# otherwise it would be very racy. So we add the regular read_timeout on top
|
|
106
|
+
# to account for the network delay.
|
|
107
|
+
timeout += config.read_timeout
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
super(timeout, command, &block)
|
|
111
|
+
rescue ::RedisClient::Error => error
|
|
112
|
+
Client.translate_error!(error)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def pipelined(exception: true)
|
|
116
|
+
super
|
|
117
|
+
rescue ::RedisClient::Error => error
|
|
118
|
+
Client.translate_error!(error)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def multi(watch: nil)
|
|
122
|
+
super
|
|
123
|
+
rescue ::RedisClient::Error => error
|
|
124
|
+
Client.translate_error!(error)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def inherit_socket!
|
|
128
|
+
@inherit_socket = true
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
module Commands
|
|
5
|
+
module Bitmaps
|
|
6
|
+
# Sets or clears the bit at offset in the string value stored at key.
|
|
7
|
+
#
|
|
8
|
+
# @param [String] key
|
|
9
|
+
# @param [Integer] offset bit offset
|
|
10
|
+
# @param [Integer] value bit value `0` or `1`
|
|
11
|
+
# @return [Integer] the original bit value stored at `offset`
|
|
12
|
+
def setbit(key, offset, value)
|
|
13
|
+
send_command([:setbit, key, offset, value])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Returns the bit value at offset in the string value stored at key.
|
|
17
|
+
#
|
|
18
|
+
# @param [String] key
|
|
19
|
+
# @param [Integer] offset bit offset
|
|
20
|
+
# @return [Integer] `0` or `1`
|
|
21
|
+
def getbit(key, offset)
|
|
22
|
+
send_command([:getbit, key, offset])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Count the number of set bits in a range of the string value stored at key.
|
|
26
|
+
#
|
|
27
|
+
# @param [String] key
|
|
28
|
+
# @param [Integer] start start index
|
|
29
|
+
# @param [Integer] stop stop index
|
|
30
|
+
# @param [String, Symbol] scale the scale of the offset range
|
|
31
|
+
# e.g. 'BYTE' - interpreted as a range of bytes, 'BIT' - interpreted as a range of bits
|
|
32
|
+
# @return [Integer] the number of bits set to 1
|
|
33
|
+
def bitcount(key, start = 0, stop = -1, scale: nil)
|
|
34
|
+
command = [:bitcount, key, start, stop]
|
|
35
|
+
command << scale if scale
|
|
36
|
+
send_command(command)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Perform a bitwise operation between strings and store the resulting string in a key.
|
|
40
|
+
#
|
|
41
|
+
# @param [String] operation e.g. `and`, `or`, `xor`, `not`
|
|
42
|
+
# @param [String] destkey destination key
|
|
43
|
+
# @param [String, Array<String>] keys one or more source keys to perform `operation`
|
|
44
|
+
# @return [Integer] the length of the string stored in `destkey`
|
|
45
|
+
def bitop(operation, destkey, *keys)
|
|
46
|
+
keys.flatten!(1)
|
|
47
|
+
command = [:bitop, operation, destkey]
|
|
48
|
+
command.concat(keys)
|
|
49
|
+
send_command(command)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Return the position of the first bit set to 1 or 0 in a string.
|
|
53
|
+
#
|
|
54
|
+
# @param [String] key
|
|
55
|
+
# @param [Integer] bit whether to look for the first 1 or 0 bit
|
|
56
|
+
# @param [Integer] start start index
|
|
57
|
+
# @param [Integer] stop stop index
|
|
58
|
+
# @param [String, Symbol] scale the scale of the offset range
|
|
59
|
+
# e.g. 'BYTE' - interpreted as a range of bytes, 'BIT' - interpreted as a range of bits
|
|
60
|
+
# @return [Integer] the position of the first 1/0 bit.
|
|
61
|
+
# -1 if looking for 1 and it is not found or start and stop are given.
|
|
62
|
+
def bitpos(key, bit, start = nil, stop = nil, scale: nil)
|
|
63
|
+
raise(ArgumentError, 'stop parameter specified without start parameter') if stop && !start
|
|
64
|
+
|
|
65
|
+
command = [:bitpos, key, bit]
|
|
66
|
+
command << start if start
|
|
67
|
+
command << stop if stop
|
|
68
|
+
command << scale if scale
|
|
69
|
+
send_command(command)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Redis
|
|
4
|
+
module Commands
|
|
5
|
+
module Cluster
|
|
6
|
+
# Sends `CLUSTER *` command to random node and returns its reply.
|
|
7
|
+
#
|
|
8
|
+
# @see https://redis.io/commands#cluster Reference of cluster command
|
|
9
|
+
#
|
|
10
|
+
# @param subcommand [String, Symbol] the subcommand of cluster command
|
|
11
|
+
# e.g. `:slots`, `:nodes`, `:slaves`, `:info`
|
|
12
|
+
#
|
|
13
|
+
# @return [Object] depends on the subcommand
|
|
14
|
+
def cluster(subcommand, *args)
|
|
15
|
+
send_command([:cluster, subcommand] + args)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Sends `ASKING` command to random node and returns its reply.
|
|
19
|
+
#
|
|
20
|
+
# @see https://redis.io/topics/cluster-spec#ask-redirection ASK redirection
|
|
21
|
+
#
|
|
22
|
+
# @return [String] `'OK'`
|
|
23
|
+
def asking
|
|
24
|
+
send_command(%i[asking])
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|