class Redis::Connection
- Redis::Connection
- Reference
- Object
Overview
The connection wraps the TCP connection to the Redis server.
Included Modules
Defined in:
connection.crgraph.cr
Constructors
-
.new(uri : URI = URI.parse("redis:///"), log : ::Log = Log)
We receive all connection information in the URI.
Macro Summary
Instance Method Summary
-
#close
Close the connection to the server.
-
#encode(command)
Send the given command over the wire without waiting for a reply.
-
#flush
Flush the connection buffer and make sure we've sent everything to the server.
- #hscan_each(key : String, match pattern : String | Nil = nil, count : String | Int | Nil = nil, &) : Nil
-
#multi(retries = 5, &)
Execute a transaction within the server.
-
#pipeline(&)
Execute a pipeline of commands.
-
#psubscribe(*channels : String, &block : Subscription, self -> )
Subscribe to the given pubsub channels.
- #punsubscribe(*channels : String)
-
#read
Read the next value from the server
-
#read?
Read the next value from the server, returning
nil
if the connection is closed. -
#readonly! : Nil
Put this connection in a readonly state.
-
#run(command, retries = 5) : Value
Execute the given command and return the result from the server.
-
#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.
-
#subscribe(*channels : String, &block : Subscription, self -> )
Subscribe to the given pubsub channels.
-
#subscribe(*channels : String)
Subscribe to the given channels without having to pass a block, which would block execution.
-
#unsubscribe
Unsubscribe this connection from all subscriptions.
-
#unsubscribe(*channels : String)
Unsubscribe this connection the given channels.
-
#url
The URI
Instance methods inherited from module Redis::Commands
decr(key : String)
decr,
decrby(key : String, amount : Int | String)
decrby,
del(keys : Enumerable(String))del(*keys : String) del, eval(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new) eval, eval_ro(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new) eval_ro, evalsha(sha : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new) evalsha, evalsha_ro(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new) evalsha_ro, exists(*keys : String) exists, expire(key : String, ttl : Time::Span)
expire(key : String, ttl : Int) expire, expireat(key : String, at : Time) expireat, flushall flushall, flushdb flushdb, ft ft, get(key : String) get, graph(key : String) graph, incr(key : String) incr, incrby(key : String, amount : Int | String) incrby, info info, json json, keys(pattern = "*") keys, mget(keys : Enumerable(String)) mget, mset(data : Hash(String, String)) mset, pexpire(key : String, ttl : Time::Span)
pexpire(key : String, ttl : Int) pexpire, pexpireat(key : String, at : Time) pexpireat, pttl(key : String) pttl, publish(channel : String, message : String) publish, run(command) run, scan(cursor : String = "0", match : String | Nil = nil, count : String | Int | Nil = nil, type : String | Nil = nil) scan, script_exists(shas : Enumerable(String))
script_exists(*shas : String) script_exists, script_flush(mode : ScriptFlushMode) script_flush, script_kill script_kill, script_load(script : String) script_load, set(key : String, value : String, ex : String | Int | Nil = nil, px : String | Int | Nil = nil, nx = false, xx = false, keepttl = false)
set(key, value, ex : Time, nx = false, xx = false, keepttl = false)
set(key, value, ex : Time::Span, nx = false, xx = false, keepttl = false) set, ts ts, ttl(key : String) ttl, type(key : String) type, unlink(keys : Enumerable(String))
unlink(*keys : String) unlink, wait(numreplicas replica_count : Int | String, timeout : Time::Span)
wait(numreplicas replica_count : Int | String, timeout : Int | String) wait
Instance methods inherited from module Redis::Commands::Stream
xack(key : String, group : String, id : String)xack(key : String, group : String, ids : Enumerable(String)) xack, xadd(key : String, id : String, maxlen, data : ::Hash(String, String))
xadd(key : String, id : String, data : ::Hash(String, String))
xadd(key : String, id : String, maxlen = nil, **data) xadd, xautoclaim(key : String, group : String, consumer : String, min_idle_time : Time::Span, start : String, count : Int | String | Nil = nil) xautoclaim, xdel(key : String, ids : Enumerable(String))
xdel(key : String, *ids : String) xdel, xgroup(command : String, key : String, groupname : String)
xgroup(command : XGroup, key : String, groupname : String, *, id : String | Nil = nil, mkstream = false, consumer_name : String | Nil = nil)
xgroup(command : String, key : String, groupname : String, *args : String) xgroup, xgroup_create(key : String, groupname : String, *, id : String = "$", mkstream = false) xgroup_create, xgroup_create_consumer(key : String, groupname : String, consumer_name : String) xgroup_create_consumer, xlen(key : String) xlen, xpending(key : String, group : String, start : String, end finish : String, count : String | Int, idle : String | Time::Span | Nil = nil)
xpending(key : String, group : String) xpending, xrange(key : String, start min : String, end max : String, count : String | Int | Nil = nil) xrange, xreadgroup(group : String, consumer : String, count : String | Int | Nil = nil, block : Time::Span | String | Int | Nil = nil, no_ack = false, streams : ::Hash(String, String) = {} of String => String)
xreadgroup(group : String, consumer : String, count : String | Int | Nil = nil, block : Time::Span | String | Int | Nil = nil, no_ack = false, streams : NamedTuple = NamedTuple.new) xreadgroup
Instance methods inherited from module Redis::Commands::SortedSet
zadd(key : String, score : String | Int64, value : String)zadd(key : String, values : Enumerable(String)) zadd, zcard(key : String) zcard, zcount(key : String, min : String, max : String) zcount, zrange(key : String, starting : String | Int64, ending : String | Int64, with_scores : Bool = false) zrange, zrangebyscore(key : String, low : String | Int64, high : String | Int64, limit : Enumerable(String) | Nil = nil) zrangebyscore, zrem(key : String, value : String)
zrem(key : String, values : Enumerable(String))
zrem(key : String, *values : String) zrem, zremrangebyrank(key : String, low : Int64, high : Int64) zremrangebyrank, zremrangebyscore(key : String, low : String | Int64, high : String | Int64) zremrangebyscore, zrevrange(key : String, starting : String | Int64, ending : String | Int64, with_scores : Bool = false) zrevrange, zscore(key : String, value : String) zscore
Instance methods inherited from module Redis::Commands::Set
sadd(key : String, *values : String)
sadd,
scard(key : String)
scard,
sdiff(first : String, second : String)
sdiff,
sinter(first : String, *others : String)
sinter,
sismember(key : String, value : String)
sismember,
smembers(key : String)
smembers,
srem(key : String, members : Enumerable(String))srem(key : String, *values : String) srem
Instance methods inherited from module Redis::Commands::List
blpop(keys : Enumerable(String), timeout : Time::Span)blpop(*keys : String, timeout : Time::Span)
blpop(*keys : String, timeout : Int | Float)
blpop(*keys : String, timeout : String) blpop, brpop(keys : Enumerable(String), timeout : Int)
brpop(*keys : String, timeout : Time::Span)
brpop(*keys : String, timeout : Number)
brpop(*keys : String, timeout : String) brpop, brpoplpush(source : String, destination : String, timeout : Time::Span)
brpoplpush(source : String, destination : String, timeout : Int | String) brpoplpush, llen(key : String) llen, lmove(from source : String, to destination : String, from_side source_side : Side, to_side destination_side : Side) lmove, lpop(key : String, count : String | Nil = nil) lpop, lpush(key : String, values : Enumerable(String))
lpush(key, *values : String) lpush, lrange(key : String, start : String | Int, finish : String | Int) lrange, lrem(key : String, count : Int, value : String) lrem, ltrim(key : String, start : String | Int, stop : String | Int)
ltrim(key : String, range : Range(String, String))
ltrim(key : String, range : Range(Int32, Int32)) ltrim, rpop(key : String) rpop, rpoplpush(source : String, destination : String) rpoplpush, rpush(key : String, values : Enumerable(String))
rpush(key, *values : String) rpush
Instance methods inherited from module Redis::Commands::Hash
hdel(key : String, fields : Enumerable(String))hdel(key : String, *fields : String) hdel, hget(key : String, field : String) hget, hgetall(key : String) hgetall, hincrby(key : String, field : String, increment : Int | String) hincrby, hmget(key : String, fields : Enumerable(String))
hmget(key : String, *fields : String) hmget, hmset(key : String, data : ::Hash(String, String)) hmset, hscan(key : String, cursor : String, *, match pattern : String | Nil = nil, count : String | Int | Nil = nil) hscan, hset(key : String, fields : Enumerable(String))
hset(key : String, fields : ::Hash(String, String))
hset(key : String, *fields : String)
hset(key : String, **fields : String) hset, hsetnx(key : String, field : String, value : String) hsetnx
Constructor Detail
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.
Macro Detail
Instance Method Detail
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:
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 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.