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.cr

Instance Method Summary

Instance Method Detail

def brpop(*keys : String, timeout : Time::Span) #

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)

[View source]
def brpop(*keys : String, timeout : Int | Float) #

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)

[View source]
def brpop(*keys : String, timeout : String) #

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)

[View source]
def decr(key : String) #

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

[View source]
def 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

redis.del "counter"
redis.decrby "counter", 2 # => -2

[View source]
def del(*keys : String) #

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

[View source]
def exists(*keys : String) #

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

[View source]
def get(key : String) #

Get the value for the specified key

redis.set "foo", "bar"
redis.get("foo") # => "bar"

[View source]
def incr(key : String) #

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

[View source]
def 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

redis.del "counter"
redis.incrby "counter", 2 # => 2

[View source]
def keys(pattern = "*") #

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"]

[View source]
def lpush(key, *values) #

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

[View source]
def publish(channel : String, message : String) #

[View source]
def rpop(key : String) #

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

[View source]
def rpoplpush(source : String, destination : String) #

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

[View source]
abstract def run(command) #

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"})

[View source]
def sadd(key : String, *values : String) #

[View source]
def scard(key : String) #

[View source]
def sdiff(first : String, second : String) #

[View source]
def set(key : String, value : String, ex = nil, px = nil, nx = false, xx = false, keepttl = false) : Nil #

Set a given key to a given value, optionally specifying time-to-live (TTL).

  • ex: TTL in seconds (mnemonic: "ex" = "expiration")
  • px: TTL in milliseconds
  • nx: 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

[View source]
def sinter(first : String, *others : String) #

[View source]
def sismember(key : String, value : String) #

[View source]
def smembers(key : String) #

[View source]
def srem(key : String, *values : String) #

[View source]
def 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 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}

[View source]
def 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 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}

[View source]
def 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 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

[View source]
def xgroup(command : String, key : String, groupname : String) #

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"

[View source]
def xgroup(command : String, key : String, groupname : String, *args : String) #

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"

[View source]
def xlen(key : String) #

Return the number of entries in the given stream


[View source]
def xrange(key : String, start min, end max, count = nil) #

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.


[View source]
def xreadgroup(group : String, consumer : String, count : String | Int | Nil = nil, streams : NamedTuple = NamedTuple.new) #

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.


[View source]