class

Redis::Connection

Inherits Redis::Commands::Immediate < Redis::Commands < Redis::Commands::Vector < Redis::Commands::HyperLogLog < Redis::Commands::Geo < Redis::Commands::Stream < Redis::Commands::SortedSet < Redis::Commands::Set < Redis::Commands::List < Redis::Commands::Hash < Reference < Object

The connection wraps the TCP connection to the Redis server.

Constructors

new(uri : URI = URI.parse(ENV.fetch("REDIS_URL", "redis:///")), log : ::Log = Log)

We receive all connection information in the URI.

SSL connections require specifying the rediss:// scheme. Password authentication uses the URI password. DB selection uses the URI path.

Source

Instance methods

close

Close the connection to the server.

Source
closed?
Source
encode(command)

Send the given command over the wire without waiting for a reply. This is useful for query pipelining or sending commands that have no return value.

WARNING: Be careful with this because you can get the client out of sync with the server. You should almost never have to use this, but it can be useful if a command like this has not been implemented yet.

Source
flush

Flush the connection buffer and make sure we've sent everything to the server.

Source
hscan_each(key : String, match pattern : String | Nil = nil, count : String | Int | Nil = nil, &) : Nil
Source
multi(retries = 5, &)

Execute a transaction within the server. The transaction is automatically committed at the end of the block or can be rolled back with Transaction#discard. Transactions are also rolled back if an exception is raised.

redis.multi do |redis|
  redis.set "foo", "bar"
  redis.incr "counter"
  raise "Oops!"
end

redis.get "foo"     # => nil
redis.get "counter" # => nil
Source
pipeline

Execute a pipeline of commands. A pipeline sends all commands to the server before reading any of the results.

redis.pipeline do |redis|
  redis.set "foo", "bar"
  redis.incr "counter"
end
Source
psubscribe(*channels : String, &block : Subscription, self -> )

Subscribe to the given pubsub channels. The block yields a subscription object and the connection. You can setup on_message, on_subscribe, and on_unsubscribe on the subscription.

redis.subscribe "channel1", "channel2" do |subscription, connection|
  subscription.on_message do |channel, message|
    if message == "unsubscribe"
      connection.unsubscribe channel
    end

    # ...
  end

  # Respond to new subscribers
  subscription.on_subscribe do |channel, sub_count|
    connection.set "sub_count:#{channel}", sub_count.to_s
  end

  # Respond to losing subscribers
  subscription.on_unsubscribe do |channel, sub_count|
    connection.set "sub_count:#{channel}", sub_count.to_s
  end
end

For more information, see the documentation for:

Source
psubscribe(*channels : String)

Subscribe to the given channel patterns without having to pass a block that would block execution. This is useful to run inside of other subscription blocks to add new subscriptions.

Source
punsubscribe(*channels : String)
Source
read

Read the next value from the server

Source
read?

Read the next value from the server, returning nil if the connection is closed.

Source
readonly!

Put this connection in a readonly state. This is typically used when talking to replicas, and used automatically by Cluster for cluster replicas.

Source
run(command, retries = 5) : Value

Execute the given command and return the result from the server. Commands must be an Enumerable.

run({"set", "foo", "bar"})
Source
scan_each(match pattern : String | Nil = nil, count : String | Int | Nil = nil, type : String | Nil = nil, &) : Nil

Iterate over keys that match the given pattern or all keys if no pattern is supplied, yielding each key to the block. This is a much more efficient way to iterate over keys than keys.each — it avoids loading every key in memory at the same time and also doesn't block the Redis server while it generates the array of all those keys.

Source
sscan_each(key : String, match pattern : String | Nil = nil, count : String | Int | Nil = nil, &) : Nil
Source
ssubscribe(*channels : String, &block : Subscription, self -> )

Subscribe to the given pubsub channels. The block yields a subscription object and the connection. You can setup on_message, on_subscribe, and on_unsubscribe on the subscription.

redis.subscribe "channel1", "channel2" do |subscription, connection|
  subscription.on_message do |channel, message|
    if message == "unsubscribe"
      connection.unsubscribe channel
    end

    # ...
  end

  # Respond to new subscribers
  subscription.on_subscribe do |channel, sub_count|
    connection.set "sub_count:#{channel}", sub_count.to_s
  end

  # Respond to losing subscribers
  subscription.on_unsubscribe do |channel, sub_count|
    connection.set "sub_count:#{channel}", sub_count.to_s
  end
end

For more information, see the documentation for:

Source
ssubscribe(*channels : String)

Subscribe to the given sharded pubsub channels without having to pass a block. This is useful to run inside of other subscription blocks to add new sharded subscriptions.

Source
subscribe(*channels : String, &block : Subscription, self -> )

Subscribe to the given pubsub channels. The block yields a subscription object and the connection. You can setup on_message, on_subscribe, and on_unsubscribe on the subscription.

redis.subscribe "channel1", "channel2" do |subscription, connection|
  subscription.on_message do |channel, message|
    if message == "unsubscribe"
      connection.unsubscribe channel
    end

    # ...
  end

  # Respond to new subscribers
  subscription.on_subscribe do |channel, sub_count|
    connection.set "sub_count:#{channel}", sub_count.to_s
  end

  # Respond to losing subscribers
  subscription.on_unsubscribe do |channel, sub_count|
    connection.set "sub_count:#{channel}", sub_count.to_s
  end
end

For more information, see the documentation for:

Source
subscribe(*channels : String)

Subscribe to the given channels without having to pass a block, which would block execution. This is useful to run inside of other subscription blocks to add new subscriptions.

Source
sunsubscribe

Unsubscribe this connection from all sharded subscriptions.

Source
sunsubscribe(*channels : String)

Unsubscribe this connection from the given sharded pubsub channels.

Source
unsubscribe

Unsubscribe this connection from all subscriptions.

Source
unsubscribe(*channels : String)

Unsubscribe this connection the given channels.

Source
unwatch

Stop watching all watched keys on this connection.

Source
url

The URI

Source
watch(*keys : String)

Watch the given keys for changes.

Source
zscan_each(key : String, match pattern : String | Nil = nil, count : String | Int | Nil = nil, & : String, String -> ) : Nil
Source