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
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.
Instance methods
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.
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
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
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:
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.
Put this connection in a readonly state. This is typically used when
talking to replicas, and used automatically by Cluster for cluster
replicas.
Execute the given command and return the result from the server. Commands must be an Enumerable.
run({"set", "foo", "bar"})
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.
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:
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.
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:
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.
Unsubscribe this connection from the given sharded pubsub channels.