A production ready Redis cluster client for Ruby.
Renoir provides compatible interface with redis-rb.
Add this line to your application's Gemfile:
gem 'renoir'
And then execute:
$ bundle
Or install it yourself as:
$ gem install renoir
You can request to a Redis cluster by instantiating Renoir::Client
with cluster_nodes
option.
require 'renoir'
rc = Renoir::Client.new(cluster_nodes: ['127.0.0.1:30001'])
puts rc.set('hoge', 123) #=> OK
puts rc.set('fuga', 456) #=> OK
puts rc.get('hoge') #=> 123
puts rc.get('fuga') #=> 456
Options for redis-rb could also be passed as an argument:
rc = Renoir::Client.new(
cluster_nodes: ['127.0.0.1:30001'],
# redis-rb options
timeout: 100,
password: 'password',
driver: :hiredis
)
Command pipelining is supported although "future" variable is not available at this point.
rc.pipeliend do |pipeline|
pipeline.set('hoge{1}', 123)
pipeline.get('hoge{1}')
pipeline.get('fuga{1}')
end
# => ["OK", "123", nil]
If pipelined commands use different slot of key, it fails without dispatching command.
You can also execute commands atomically with #multi
:
rc.multi do |pipeline|
pipeline.set('hoge{1}', 123)
pipeline.get('hoge{1}')
pipeline.get('fuga{1}')
end
# => ["OK", "123", nil]
Renoir dispatches a command only if a slot is determined by the command. This also includes no-keys commands like KEYS
, BGSAVE
and so on.
If you would like to dispatch such commands, Renoir::Client#each_node
could be used:
keys = []
rc.each_node do |node|
keys += node.keys('test_*')
end
p keys
Following options could be passed to Renoir::Client#new
:
Array
of cluster node locations. At least one location must be specified.
A location could be String
, Array
or Hash
:
cluster_nodes: [
'127.0.0.1:30001',
['127.0.0.1', 30002],
{ host: 127.0.0.1, port: 30003 }
]
Max number of redirections. Defaults to 10
.
Max number of acceptable connection errors. Defaults to 5
.
Options for adjusting an interval of retry that a client tries to reconnect to same node when connection error is occurred.
Defaults to 0.001
(sec) and 0.1
respectively.
A retry interval is proportional to a random value sampled from [connect_retry_interval - connect_retry_random_factor, connect_retry_interval + connect_retry_random_factor]
.
Adapter name of internal connection that client uses to connect to Redis node. Defaults to :redis
.
Available adapter is :redis
so far.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/saidie/renoir. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
The original code is from antirez/redis-rb-cluster.