class

Redis

Inherits Redis::CommandExecution::ValueOriented / Redis::Commands / Reference / Object

The class is the main entry point for the Redis client.

How to use:

Require the package:

require "redis"

Then instantiate this client class:

redis = Redis.new

Then you can call Redis commands on the redis object:

redis.set("foo", "bar")
redis.get("foo")
redis.incr("visitors")

See the mixin module Commands for most of the available Redis commands such as #incr, #rename, and so on.

Multithreading / Coroutines

Please mind that a Redis object can't be shared across multiple threads/coroutines! Each thread/coroutine that wants to talk to Redis needs its own Redis object instance.

Constructors

new(host : String = "localhost", port : Int32 = 6379, unixsocket : String | Nil = nil, password : String | Nil = nil, database : Int32 | Nil = nil, url = nil, ssl : Bool = false, ssl_context : OpenSSL::SSL::Context::Client | Nil = nil, dns_timeout : Time::Span | Nil = nil, connect_timeout : Time::Span | Nil = nil, reconnect : Bool = true, command_timeout : Time::Span | Nil = nil, namespace : String = "")

Opens a Redis connection

Options:

  • host - the host to connect to
  • port - the port to connect to
  • unixsocket - instead of using TCP, you can connect to Redis via a Unix domain socket by passing its path here (e.g. "/tmp/redis.sock")
  • password - the password for authentication against the server. This is a convenience which saves you the extra call to the Redis auth command.
  • database - the number of the database to select. This a convenience which saves you a call a call to #select.
  • ssl - whether SSL should be enabled.
  • ssl_context - a OpenSSL::SSL::Context::Client.
  • dns_timeout - the dns timeout.
  • connect_timeout - the connect timeout.
  • command_timeout - the command timeout - applies when a command takes too long because the Redis-server is blocked by another command or by a dump.
  • reconnect - whether we should reconnect when we encounter a disconnected Redis connection.
  • url - Redis url. If this is given, it overrides all others.

Example:

redis = Redis.new
redis.incr("counter")
redis.close

Example:

redis = Redis.new(host: "localhost", port: 6379)
...

Example:

redis = Redis.new(unixsocket: "/tmp/redis.sock")
...

Example:

redis = Redis.new(url: "redis://:my-secret-pw@my.redis.com:6380/my-database")
...
Source

Class methods

open(host = "localhost", port = 6379, unixsocket = nil, password = nil, database = nil, url = nil, ssl = false, ssl_context = nil, dns_timeout = nil, connect_timeout = nil, reconnect = true, command_timeout = nil, namespace : String = "", &)

Opens a Redis connection, yields the given block with a Redis object and closes the connection.

Options:

  • host - the host to connect to
  • port - the port to connect to
  • unixsocket - instead of using TCP, you can connect to Redis via a Unix domain socket by passing its path here (e.g. "/tmp/redis.sock")
  • password - the password for authentication against the server. This is a convenience which saves you the extra call to the Redis auth command.
  • database - the number of the database to select. This a convenience which saves you a call a call to #select.
  • ssl - whether SSL should be enabled.
  • ssl_context - a OpenSSL::SSL::Context::Client.
  • dns_timeout - the dns timeout.
  • connect_timeout - the connect timeout.
  • command_timeout - the command timeout - applies when a command takes too long because the Redis-server is blocked by another command or by a dump.
  • reconnect - whether we should reconnect when we encounter a disconnected Redis connection.
  • url - Redis url. If this is given, it overrides all others.

Example:

Redis.open do |redis|
  redis.incr("counter")
end
Source

Instance methods

close

Closes the Redis connection.

Source
multi

Sends Redis commands in transaction mode.

Yields its block. The block receives as argument an object that has the same API as this class, except:

  • it participates in the transaction
  • all the Redis commands return Futures
  • there is an additional method #discard that will abort the transaction.

Return value: an array with all the responses

  • one element for each executed command.

Example:

redis.multi do |multi|
  multi.set("foo1", "first")
  multi.set("foo2", "second")
end

See the examples repository for more examples.

Source
pipelined

Sends Redis commands in pipeline mode.

Yields its block. The block receives as argument an object that has the same API as this class, except it participates in pipelining and all Redis commands return Futures.

Return value: an array with all the responses

  • one element for each executed command.

Example:

redis.pipelined do |pipeline|
  pipeline.set("foo1", "first")
  pipeline.set("foo2", "second")
end

See the examples repository for more examples.

Source
url

Returns the server URL for this client.

Source

Nested types