|
1 | 1 | # redis-rb [![Build Status][gh-actions-image]][gh-actions-link] [![Inline docs][inchpages-image]][inchpages-link]
|
2 | 2 |
|
3 |
| -A Ruby client that tries to match [Redis][redis-home]' API one-to-one, while still |
4 |
| -providing an idiomatic interface. |
| 3 | +A Ruby client that tries to match [Redis][redis-home]' API one-to-one, while still providing an idiomatic interface. |
5 | 4 |
|
6 | 5 | See [RubyDoc.info][rubydoc] for the API docs of the latest published gem.
|
7 | 6 |
|
|
38 | 37 | The client expects passwords with special chracters to be URL-encoded (i.e.
|
39 | 38 | `CGI.escape(password)`).
|
40 | 39 |
|
41 |
| -By default, the client will try to read the `REDIS_URL` environment variable |
42 |
| -and use that as URL to connect to. The above statement is therefore equivalent |
43 |
| -to setting this environment variable and calling `Redis.new` without arguments. |
44 |
| - |
45 | 40 | To connect to Redis listening on a Unix socket, try:
|
46 | 41 |
|
47 | 42 | ```ruby
|
@@ -76,6 +71,28 @@ redis.get("mykey")
|
76 | 71 | All commands, their arguments, and return values are documented and
|
77 | 72 | available on [RubyDoc.info][rubydoc].
|
78 | 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 heavilly recommended to use the [`connection_pool` gem], e.g.: |
| 81 | + |
| 82 | +```ruby |
| 83 | +module MyApp |
| 84 | + def self.redis |
| 85 | + @redis ||= ConnectionPool.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 | +[`connection_pool` gem](https://github.com/mperham/connection_pool) |
| 95 | + |
79 | 96 | ## Sentinel support
|
80 | 97 |
|
81 | 98 | The client is able to perform automatic failover by using [Redis
|
@@ -118,21 +135,6 @@ redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, role: :master)
|
118 | 135 |
|
119 | 136 | [Clustering](https://redis.io/topics/cluster-spec). is supported via the [`redis_cluster` gem](redis_cluster/).
|
120 | 137 |
|
121 |
| -## Storing objects |
122 |
| - |
123 |
| -Redis "string" types can be used to store serialized Ruby objects, for |
124 |
| -example with JSON: |
125 |
| - |
126 |
| -```ruby |
127 |
| -require "json" |
128 |
| - |
129 |
| -redis.set "foo", [1, 2, 3].to_json |
130 |
| -# => OK |
131 |
| - |
132 |
| -JSON.parse(redis.get("foo")) |
133 |
| -# => [1, 2, 3] |
134 |
| -``` |
135 |
| - |
136 | 138 | ## Pipelining
|
137 | 139 |
|
138 | 140 | When multiple commands are executed sequentially, but are not dependent,
|
@@ -190,15 +192,16 @@ pipeline has successfully executed, all futures are assigned their
|
190 | 192 | respective replies and can be used.
|
191 | 193 |
|
192 | 194 | ```ruby
|
| 195 | +set = incr = nil |
193 | 196 | redis.pipelined do |pipeline|
|
194 |
| - @set = pipeline.set "foo", "bar" |
195 |
| - @incr = pipeline.incr "baz" |
| 197 | + set = pipeline.set "foo", "bar" |
| 198 | + incr = pipeline.incr "baz" |
196 | 199 | end
|
197 | 200 |
|
198 |
| -@set.value |
| 201 | +set.value |
199 | 202 | # => "OK"
|
200 | 203 |
|
201 |
| -@incr.value |
| 204 | +incr.value |
202 | 205 | # => 1
|
203 | 206 | ```
|
204 | 207 |
|
@@ -277,6 +280,16 @@ Redis.new(reconnect_attempts: [
|
277 | 280 | ])
|
278 | 281 | ```
|
279 | 282 |
|
| 283 | +If you wish to disable reconnection only for some commands, you can use |
| 284 | +`disable_reconnection`: |
| 285 | + |
| 286 | +```ruby |
| 287 | +redis.get("some-key") # this may be retried |
| 288 | +redis.disable_reconnection do |
| 289 | + redis.incr("some-counter") # this won't be retried. |
| 290 | +end |
| 291 | +``` |
| 292 | + |
280 | 293 | ## SSL/TLS Support
|
281 | 294 |
|
282 | 295 | To enable SSL support, pass the `:ssl => true` option when configuring the
|
@@ -312,13 +325,7 @@ redis = Redis.new(
|
312 | 325 | )
|
313 | 326 | ```
|
314 | 327 |
|
315 |
| -[stunnel]: https://www.stunnel.org/ |
316 |
| -[hitch]: https://hitch-tls.org/ |
317 |
| -[ghostunnel]: https://github.com/square/ghostunnel |
318 |
| -[OpenSSL::SSL::SSLContext documentation]: http://ruby-doc.org/stdlib-2.3.0/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html |
319 |
| - |
320 |
| -*NOTE:* SSL is only supported by the default "Ruby" driver |
321 |
| - |
| 328 | +[OpenSSL::SSL::SSLContext documentation]: http://ruby-doc.org/stdlib-2.5.0/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html |
322 | 329 |
|
323 | 330 | ## Expert-Mode Options
|
324 | 331 |
|
@@ -351,7 +358,17 @@ gem "redis"
|
351 | 358 | gem "hiredis-client"
|
352 | 359 | ```
|
353 | 360 |
|
354 |
| -When instantiating the client object, specify hiredis: |
| 361 | +If your application doesn't call `Bundler.require`, you may have |
| 362 | +to require it explictly: |
| 363 | + |
| 364 | +```ruby |
| 365 | +require "hiredis-client" |
| 366 | +```` |
| 367 | + |
| 368 | +This makes the hiredis driver the default. |
| 369 | + |
| 370 | +If you want to be certain hiredis is being used, when instantiating |
| 371 | +the client object, specify hiredis: |
355 | 372 |
|
356 | 373 | ```ruby
|
357 | 374 | redis = Redis.new(driver: :hiredis)
|
|
0 commit comments