redis-cluster

redis-cluster client redis redis-cluster
0.8.6 Latest release released

CI

redis-cluster library for Crystal.

  • supported versions : 0.27.2 0.31.1 0.32.1 0.33.0 0.34.0, 0.35.1, 0.36.1, 1.0.0
  • crystal-redis : using forked maiha/crystal-redis

Classes

  • Redis : a redis standard client (stefanwille/crystal-redis)
  • Redis::Cluster : a redis cluster client (in this library)
  • Redis::Client : a hybrid proxy to above clients (in this library)

Supported API

See API

Installation

Add this to your application's shard.yml:

dependencies:
  redis-cluster:
    github: maiha/redis-cluster.cr
    version: 0.8.6

Usage

Redis::Cluster client

  • assumes that our cluster is runing on localhost:7001 and 7002, ...
require "redis-cluster"

bootstrap = "127.0.0.1:7001,127.0.0.1:7002"
cluster = Redis::Cluster.new(bootstrap)
# cluster = Redis::Cluster.new(bootstrap, password: "secret")
# cluster = Redis::Cluster.new(bootstrap, password: "secret", ssl: true)

cluster.set "foo", "123"
cluster.get "foo"         # => "123"
cluster.counts.values     # => [0, 0, 1]

cluster.close

methods

See crystal-redis because most of all methods are thin proxy to it.

Redis client (enhancement)

This library also add some features to standard Redis libarary.

Redis::Client

This class is a high level hybrid client which can speak to both standard and clustered redis nodes. And it also has a reconnecting feature. Well, we don't care anything about the node is restarted or clustered or not.

So, the following code works on either redis mode.

redis = Redis::Client.new(host: "127.0.0.1", port: 6379)
redis.get("foo")

TLS/SSL Support

The library now supports secure connections via TLS/SSL for both standard Redis and cluster configurations:

Using rediss:// URLs

# Single secure connection
cluster = Redis::Cluster.new("rediss://password@secure-redis.example.com:6380")

# Multiple secure nodes
cluster = Redis::Cluster.new("rediss://password@node1:6380,rediss://password@node2:6380")

Using SSL flags

# Basic SSL connection
bootstrap = Redis::Cluster::Bootstrap.new(
  host: "secure-redis.example.com",
  port: 6380,
  pass: "password",
  ssl: true
)
cluster = Redis::Cluster::Client.new(bootstrap)

# With custom SSL context
ssl_context = OpenSSL::SSL::Context::Client.new
ssl_context.verify_mode = OpenSSL::SSL::VerifyMode::PEER
ssl_context.ca_certificates = "/path/to/ca-certificates.crt"

bootstrap = Redis::Cluster::Bootstrap.new(
  host: "secure-redis.example.com",
  port: 6380,
  pass: "password",
  ssl: true,
  ssl_context: ssl_context
)
cluster = Redis::Cluster::Client.new(bootstrap)

Hybrid client with TLS

redis = Redis::Client.new(
  host: "secure-redis.example.com",
  port: 6380,
  password: "password",
  ssl: true
)

TLS with Unix sockets

# Secure Unix socket connection
bootstrap = Redis::Cluster::Bootstrap.new(
  sock: "/tmp/redis-secure.sock",
  pass: "password",
  ssl: true
)
cluster = Redis::Cluster::Client.new(bootstrap)

multi and pipelined

Redis::Client#multi and pipelined requires a key to resolve redis node. In standard redis, the information is just ignored but it is necessary for hybrid api.

redis.pipelined("foo") do |api|
  api.set("foo", 1)
  api.set("bar", 2) # raises unless both "foo" and "bar" are in the same node
end

When reconnect option is set, operations will be executed once again after connection error or hash slot error.

redis.pipelined("foo", reconnect: true) do |api|
  ...

Redis#each

Handy SCAN especially for block.

redis = Redis.new
redis.each(count: 1000) |key|
redis.each_keys(count: 1000) |keys|

RESTRICTION

  • multi needs key for its first arg to resolve master node
redis.multi("foo1") do |multi|
  multi.set("foo1", "first")
  multi.set("foo2", "second")
end

Roadmap

v0.8.0

  • [ ] define method explicitly
  • [x] Commands : Pipeline

Contributing

  1. Fork it ( https://github.com/maiha/redis-cluster.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

redis-cluster:
  github: maiha/redis-cluster.cr
  version: ~> 0.8.6
License MIT
Crystal none

Authors

Dependencies 2

  • crc16 >= 0.1.0
    {'github' => 'maiha/crc16.cr', 'version' => '>= 0.1.0'}
  • redis e81d33945592688c07add1274b811c06d0e4ee26
    {'commit' => 'e81d33945592688c07add1274b811c06d0e4ee26', 'github' => 'stefanwille/crystal-redis'}

Development Dependencies 0

Last synced .
search fire star recently