module Redis::Commands
Overview
All Redis commands are defined in this module. Any paradigm that needs to
use these commands simply overrides #run
, which takes a single command
object, which must be an Enumerable
.
TODO Add more Redis commands from https://redis.io/commands
Direct including types
Defined in:
commands.crInstance Method Summary
-
#blpop(*keys : String, timeout : Time::Span)
Remove and return an element from the end of the given list.
-
#blpop(*keys : String, timeout : Int | Float)
Remove and return an element from the end of the given list.
-
#blpop(*keys : String, timeout : String)
Remove and return an element from the end of the given list.
- #brpop(keys : Enumerable(String), timeout : Int)
-
#brpop(*keys : String, timeout : Time::Span)
Remove and return an element from the end of the given list.
-
#brpop(*keys : String, timeout : Number)
Remove and return an element from the end of the given list.
-
#brpop(*keys : String, timeout : String)
Remove and return an element from the end of the given list.
-
#decr(key : String)
Atomically decrement and return the integer value for the specified key, creating it if it does not exist
-
#decrby(key : String, amount : Int | String)
Atomically decrement and return the integer value for the specified key by the specified amount, creating it if it does not exist
-
#del(*keys : String)
Delete all specified keys and return the number of keys deleted.
-
#exists(*keys : String)
Return the number of specified keys that exist
- #expire(key, ttl : Int)
- #flushdb
-
#get(key : String)
Get the value for the specified key
- #hget(key : String, field : String)
- #hgetall(key : String)
- #hmget(key : String, *fields : String)
- #hmset(key : String, data : Hash(String, String))
- #hset(key : String, field : String, value : String)
-
#incr(key : String)
Atomically increment and return the integer value for the specified key, creating it if it does not exist
-
#incrby(key : String, amount : Int | String)
Atomically increment and return the integer value for the specified key by the specified amount, creating it if it does not exist
- #info
-
#keys(pattern = "*")
Get the keys whose names follow the specified glob pattern.
- #llen(key : String)
-
#lpop(key : String, count : String | Nil = nil)
Remove an item from the beginning of a list, returning the item or
nil
if the list was empty. - #lpush(key : String, values : Enumerable(String))
-
#lpush(key, *values : String)
Insert an item at the beginning of a list, returning the number of items in the list after the insert.
- #lrange(key : String, starting : String, ending : String)
- #lrem(key : String, count : Int, value : String)
- #mget(keys : Enumerable(String))
- #mset(data : Hash(String, String))
- #publish(channel : String, message : String)
-
#rpop(key : String)
Remove and return an element from the end of the given list.
-
#rpoplpush(source : String, destination : String)
Atomically remove an item from the end of a list and insert it at the beginning of another.
-
#rpush(key, *values : String)
Insert an item at the end of a list, returning the number of items in the list after the insert.
-
#run(command)
Execute the given command and return the result from the server.
- #sadd(key : String, *values : String)
- #scard(key : String)
- #sdiff(first : String, second : String)
-
#set(key : String, value : String, ex : String | Int | Nil = nil, px : String | Int | Nil = nil, nx = false, xx = false, keepttl = false)
Set a given key to a given value, optionally specifying time-to-live (TTL).
- #set(key, value, ex : Time, nx = false, xx = false, keepttl = false)
- #set(key, value, ex : Time::Span, nx = false, xx = false, keepttl = false)
- #sinter(first : String, *others : String)
- #sismember(key : String, value : String)
- #smembers(key : String)
- #srem(key : String, members : Enumerable(String))
- #srem(key : String, *values : String)
- #type(key : String)
-
#xadd(key : String, id : String, maxlen, data : Hash(String, String))
Append an entry with the specified data to the stream with the given
key
and gives it the specifiedid
. -
#xadd(key : String, id : String, data : Hash(String, String))
Append an entry with the specified data to the stream with the given
key
and gives it the specifiedid
. -
#xadd(key : String, id : String, maxlen = nil, **data)
Append an entry with the specified data to the stream with the given
key
and gives it the specifiedid
. -
#xgroup(command : String, key : String, groupname : String)
Run a Redis XGROUP subcommand for a given stream.
-
#xgroup(command : String, key : String, groupname : String, *args : String)
Run a Redis XGROUP subcommand for a given stream.
-
#xlen(key : String)
Return the number of entries in the given stream
-
#xrange(key : String, start min, end max, count = nil)
Return the entries in the given stream between the
start
andend
ids. -
#xreadgroup(group : String, consumer : String, count : String | Int | Nil = nil, block : Time::Span | String | Int | Nil = nil, no_ack = false, streams : NamedTuple = NamedTuple.new)
Execute an
XREADGROUP
command on the Redis server. - #zadd(key : String, score : String | Float, value : String)
- #zadd(key : String, values : Enumerable)
- #zcard(key : String)
- #zrange(key : String, starting : String | Int, ending : String | Int, with_scores : Bool = false)
- #zrangebyscore(key : String, low : String | Float, high : String | Float, limit : Enumerable(String) | Nil = nil)
- #zrem(key : String, value : String)
- #zremrangebyrank(key : String, low : Int, high : Int)
- #zremrangebyscore(key : String, low : String | Float, high : String | Float)
- #zrevrange(key : String, starting : String | Int, ending : String | Int, with_scores : Bool = false)
Instance Method Detail
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method waits the specified amount
of time for an element to be added to it by another connection. If the
element is added by another connection within that amount of time, this
method will return it immediately. If it is not, then this method returns
nil
.
redis.rpush "foo", "first"
spawn do
sleep 100.milliseconds
redis.rpush "foo", "second"
end
redis.blpop "foo", 1.second # => "first"
redis.blpop "foo", 1.second # => "second" (after 100 milliseconds)
redis.blpop "foo", 1.second # => nil (after 1 second)
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method waits the specified number
of seconds for an element to be added to it by another connection. If the
element is added by another connection within that number of seconds,
this method will return it immediately. If it is not, then this method
returns nil
.
redis.lpush "foo", "first"
spawn do
sleep 100.milliseconds
redis.lpush "foo", "second"
end
redis.blpop "foo", 1 # => "first"
redis.blpop "foo", 1 # => "second" (after 100 milliseconds)
redis.blpop "foo", 1 # => nil (after 1 second)
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method waits the specified number
of seconds for an element to be added to it by another connection. If the
element is added by another connection within that number of seconds,
this method will return it immediately. If it is not, then this method
returns nil
.
redis.lpush "foo", "first"
spawn do
sleep 100.milliseconds
redis.lpush "foo", "second"
end
redis.blpop "foo", "1" # => "first"
redis.blpop "foo", "1" # => "second" (after 100 milliseconds)
redis.blpop "foo", "1" # => nil (after 1 second)
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method waits the specified amount
of time for an element to be added to it by another connection. If the
element is added by another connection within that amount of time, this
method will return it immediately. If it is not, then this method returns
nil
.
redis.lpush "foo", "first"
spawn do
sleep 100.milliseconds
redis.lpush "foo", "second"
end
redis.brpop "foo", 1.second # => "first"
redis.brpop "foo", 1.second # => "second" (after 100 milliseconds)
redis.brpop "foo", 1.second # => nil (after 1 second)
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method waits the specified number
of seconds for an element to be added to it by another connection. If the
element is added by another connection within that number of seconds,
this method will return it immediately. If it is not, then this method
returns nil
.
redis.lpush "foo", "first"
spawn do
sleep 100.milliseconds
redis.lpush "foo", "second"
end
redis.brpop "foo", 1 # => "first"
redis.brpop "foo", 1 # => "second" (after 100 milliseconds)
redis.brpop "foo", 1 # => nil (after 1 second)
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method waits the specified number
of seconds for an element to be added to it by another connection. If the
element is added by another connection within that number of seconds,
this method will return it immediately. If it is not, then this method
returns nil
.
redis.lpush "foo", "first"
spawn do
sleep 100.milliseconds
redis.lpush "foo", "second"
end
redis.brpop "foo", "1" # => "first"
redis.brpop "foo", "1" # => "second" (after 100 milliseconds)
redis.brpop "foo", "1" # => nil (after 1 second)
Atomically decrement and return the integer value for the specified key, creating it if it does not exist
redis.del "counter"
redis.decr "counter" # => -1
Atomically decrement and return the integer value for the specified key by the specified amount, creating it if it does not exist
redis.del "counter"
redis.decrby "counter", 2 # => -2
Delete all specified keys and return the number of keys deleted.
redis.set "foo", "12"
redis.del "foo", "bar" # => 1
redis.del "foo", "bar" # => 0
Return the number of specified keys that exist
redis.exists("foo", "bar") # => 0
redis.set "foo", "exists now"
redis.exists("foo", "bar") # => 1
redis.set "bar", "also exists now"
redis.exists("foo", "bar") # => 2
Get the value for the specified key
redis.set "foo", "bar"
redis.get("foo") # => "bar"
Atomically increment and return the integer value for the specified key, creating it if it does not exist
redis.del "counter"
redis.incr "counter" # => 1
Atomically increment and return the integer value for the specified key by the specified amount, creating it if it does not exist
redis.del "counter"
redis.incrby "counter", 2 # => 2
Get the keys whose names follow the specified glob pattern. If a pattern is not specified, it will return all keys by default. Be careful when using this command on Redis servers with a lot of traffic and millions of keys.
redis.keys # => ["foo", "bar", "baz"]
redis.keys("f*") # => ["foo"]
redis.keys("b*") # => ["bar", "baz"]
Remove an item from the beginning of a list, returning the item or nil
if the list was empty.
redis.del "my-list" # Delete so we know it's empty
redis.lpush "my-list", "foo"
redis.lpop "my-list" # => "foo"
redis.lpop "my-list" # => nil
Insert an item at the beginning of a list, returning the number of items in the list after the insert.
redis.del "my-list" # Delete so we know it's empty
redis.lpush "my-list", "foo", "bar" # => 2
redis.lpush "my-list", "foo", "bar" # => 4
Remove and return an element from the end of the given list. If the list
is empty or the key does not exist, this method returns nil
redis.lpush "foo", "hello"
redis.rpop "foo" # => "hello"
redis.rpop "foo" # => nil
Atomically remove an item from the end of a list and insert it at the
beginning of another. Returns that list item. If the first list is empty,
nothing happens and this method returns nil
.
redis.del "foo"
redis.lpush "foo", "hello", "world"
redis.rpoplpush "foo", "bar" # => "hello"
redis.rpoplpush "foo", "bar" # => "world"
redis.rpoplpush "foo", "bar" # => nil
Insert an item at the end of a list, returning the number of items in the list after the insert.
redis.del "my-list" # Delete so we know it's empty
redis.rpush "my-list", "foo", "bar" # => 2
redis.rpush "my-list", "foo", "bar" # => 4
Execute the given command and return the result from the server. Commands
must be an Enumerable
and its size
method must be re-entrant.
run({"set", "foo", "bar"})
Set a given key to a given value, optionally specifying time-to-live (TTL).
ex
: TTL in seconds (mnemonic: "ex" = "expiration")px
: TTL in millisecondsnx
: Only set this key if it does not exist (mnemonic: "nx" = it does "not exist")xx
: only set this key if it does exist (mnemonic: "xx" = it "exists exists" — look, I don't make the rules)keepttl
: If there is a TTL already set on the key, retain that TTL instead of overwriting it
redis.set "foo", "bar", ex: 1
redis.get("foo") # => "bar"
sleep 1.second
redis.get("foo") # => nil
Append an entry with the specified data to the stream with the given key
and gives it the specified id
. If the id is "*"
, Redis will assign it
an id of the form "#{Time.utc.to_unix_ms}-#{autoincrementing_index}"
.
If maxlen
is provided, Redis will trim the stream to the specified
length. If maxlen
is of the form ~ 1000
, Redis will trim it to
approximately that length, removing entries when it can do so
efficiently. This method returns the id
that Redis stores.
redis.xadd "my-stream", "*", {"name" => "foo", "id" => UUID.random.to_s}
Append an entry with the specified data to the stream with the given key
and gives it the specified id
. If the id is "*"
, Redis will assign it
an id of the form "#{Time.utc.to_unix_ms}-#{autoincrementing_index}"
.
If maxlen
is provided, Redis will trim the stream to the specified
length. If maxlen
is of the form ~ 1000
, Redis will trim it to
approximately that length, removing entries when it can do so
efficiently. This method returns the id
that Redis stores.
redis.xadd "my-stream", "*", {"name" => "foo", "id" => UUID.random.to_s}
Append an entry with the specified data to the stream with the given key
and gives it the specified id
. If the id is "*"
, Redis will assign it
an id of the form "#{Time.utc.to_unix_ms}-#{autoincrementing_index}"
.
If maxlen
is provided, Redis will trim the stream to the specified
length. If maxlen
is of the form ~ 1000
, Redis will trim it to
approximately that length, removing entries when it can do so
efficiently. This method returns the id
that Redis stores.
redis.xadd "my-stream", "*", name: "foo", id: UUID.random.to_s
Run a Redis XGROUP subcommand for a given stream. See the XGROUP command in the Redis documentation for more information.
redis.xgroup "DESTROY", "my-stream", "my-group"
Run a Redis XGROUP subcommand for a given stream. See the XGROUP command in the Redis documentation for more information.
redis.xgroup "CREATE", "my-stream", "my-group", "0"
Return the entries in the given stream between the start
and end
ids.
If count
is provided, Redis will return only that number of entries.
Execute an XREADGROUP
command on the Redis server.
TODO Make the return value of this command easier to work with. Nested heterogeneous arrays aren't easy to work with.