module

Redis::Commands

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

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

Instance methods

bf

Return a BloomFilter, allowing you to run commands for Bloom Filters.

redis.bf.add "my-bloom-filter", "value"
Source
dbsize

Send a DBSIZE command to the server, returning the number of keys in the currently-selected database.

Source
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
Source
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
Source
del(keys : Enumerable(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
Source
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
Source
dump(key : String)
Source
eval(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)

Evaluate the given Lua script, either referenced by SHA with evalsha or directly with eval.

NOTE: Use eval only for very trivial scripts and evalsha for larger or frequently executed scripts to amortize parse/compile time as well as send fewer bytes along the wire.

NOTE: Use eval_ro and evalsha_ro in a clustered environment to evaluate the scripts on read-only replicas.

script = <<-LUA
  return "this script was " + ARGV[1]
LUA

sha = redis.script_load(script)
redis.eval(script, args: ["evaluated on the fly"]
redis.evalsha(sha, args: ["precompiled"])
Source
eval_ro(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)

Evaluate the given Lua script, either referenced by SHA with evalsha or directly with eval.

NOTE: Use eval only for very trivial scripts and evalsha for larger or frequently executed scripts to amortize parse/compile time as well as send fewer bytes along the wire.

NOTE: Use eval_ro and evalsha_ro in a clustered environment to evaluate the scripts on read-only replicas.

script = <<-LUA
  return "this script was " + ARGV[1]
LUA

sha = redis.script_load(script)
redis.eval(script, args: ["evaluated on the fly"]
redis.evalsha(sha, args: ["precompiled"])
Source
evalsha(sha : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)

Evaluate the given Lua script, either referenced by SHA with evalsha or directly with eval.

NOTE: Use eval only for very trivial scripts and evalsha for larger or frequently executed scripts to amortize parse/compile time as well as send fewer bytes along the wire.

NOTE: Use eval_ro and evalsha_ro in a clustered environment to evaluate the scripts on read-only replicas.

script = <<-LUA
  return "this script was " + ARGV[1]
LUA

sha = redis.script_load(script)
redis.eval(script, args: ["evaluated on the fly"]
redis.evalsha(sha, args: ["precompiled"])
Source
evalsha_ro(sha : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)

Evaluate the given Lua script, either referenced by SHA with evalsha or directly with eval.

NOTE: Use eval only for very trivial scripts and evalsha for larger or frequently executed scripts to amortize parse/compile time as well as send fewer bytes along the wire.

NOTE: Use eval_ro and evalsha_ro in a clustered environment to evaluate the scripts on read-only replicas.

script = <<-LUA
  return "this script was " + ARGV[1]
LUA

sha = redis.script_load(script)
redis.eval(script, args: ["evaluated on the fly"]
redis.evalsha(sha, args: ["precompiled"])
Source
exists(keys : Enumerable(String))

Return the number of specified keys that exist

redis.exists(%w[foo bar]) # => 0
redis.set "foo", "exists now"
redis.exists(%w[foo bar]) # => 1
redis.set "bar", "also exists now"
redis.exists(%w[foo bar]) # => 2
Source
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
Source
expire(key : String, ttl : Time::Span)
Source
expire(key : String, ttl : Int)
Source
expireat(key : String, at : Time)
Source
flushall

Delete all the keys of all the existing databases, not just the currently selected one.

Source
flushdb

Delete all the keys of the currently selected DB

Source
ft

EXPERIMENTAL RediSearch support is still under development. Some APIs may change while details are discovered.

Source
get(key : String)

Get the value for the specified key

redis.set "foo", "bar"
redis.get("foo") # => "bar"
Source
getdel(key : String)

Delete key and return its value, similar to Hash#delete in Crystal.

Source
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
Source
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
Source
incrbyfloat(key : String, amount : Float | String)

Atomically increment and return the floating-point value for key by amount, creating it as if it were 0 if it does not exist.

redis.del "metric"
redis.incrbyfloat "metric", 4.2 # => "4.2"
redis.incrbyfloat "metric", 6.9 # => "11.1"

NOTE: The RESP2 protocol used by Redis does not support encoding floating- point numbers, so the server will return this value as a string.

Source
info(section : String)
Source
json

Return a Redis::JSON instance that wraps the current Redis::Client or Redis::Cluster.

Source
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"]
Source
mget(keys : Enumerable(String))
Source
mget(*keys : String)
Source
mset(data : ::Hash(String, String))
Source
pexpire(key : String, ttl : Time::Span)
Source
pexpire(key : String, ttl : Int)
Source
pexpireat(key : String, at : Time)
Source
ping(message : String | Nil = nil)

Send a PING command to the server, returning message if it is provided or "PONG" otherwise.

Source
pttl(key : String)
Source
publish(channel : String, message : String)
Source
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"})
Source
scan(cursor : String = "0", match : String | Nil = nil, count : String | Int | Nil = nil, type : String | Nil = nil)
Source
script_exists(shas : Enumerable(String))

Return an array where each entry is 1 if the corresponding entry in the list of shas exists or 0 if it does not.

Source
script_exists(*shas : String)

Return an array where each entry is 1 if the corresponding entry in the list of shas exists or 0 if it does not.

Source
script_flush(mode : ScriptFlushMode)

Flush the Lua scripts cache, see the official docs.

redis.script_flush :async # Flush scripts asynchronously
redis.script_flush :sync  # Flush scripts immediately
Source
script_flush

Flush the Lua scripts cache, see the official docs.

redis.script_flush
Source
script_kill

Kill the currently executing eval script, assuming no write operation was yet performed by the script.

Source
script_load(script : String)

Preload a Lua script, returning the SHA of the script to pass to evalsha.

sha = redis.script_load(<<-LUA)
  return "Hello " + ARGV[1]
LUA

redis.evalsha(sha, args: ["world"]) # => "Hello world"
Source
set(key, value, *, ex : Time, nx = false, xx = false, keepttl = false, get = false)

Sets the value stored at key to value, expiring at the given Time with millisecond precision.

Source
set(key, value, *, ex : Time::Span, nx = false, xx = false, keepttl = false, get = false)

Sets the value stored at key to value, expiring after the given Time::Span with millisecond precision.

Source
set(key : String, value : String | Bytes, *, ex : String | Int | Nil = nil, px : String | Int | Nil = nil, nx = false, xx = false, keepttl = false, get = false)

Set the value stored at key to value, optionally specifying expiration.

  • 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)
  • get: Return the previous value stored in key, providing the functionality in GETSET
  • ex: TTL in seconds (mnemonic: "ex" = "expiration")
  • px: TTL in milliseconds
  • 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

# Does not overwrite when `nx` is truthy
redis.set "foo", "1", nx: true # => "OK"
redis.set "foo", "2", nx: true # => nil

# Does not create a key when `xx` is truthy
redis.del "update-only"
redis.set "update-only", "this will not be set", xx: true # => nil

# Returns the previous value when `get` is truthy
redis.set "key", "value"                # => "OK"
redis.set "key", "new-value", get: true # => "value"
redis.get "key"                         # => "new-value"

NOTE: nx and xx are mutually exclusive, as are ex and px. They exist in the same method signature only to avoid an explosion of set implementations.

Source
strlen(key : String)

Return the number of bytes in the string stored in key, returns 0 if the key does not exist.

redis.set "foo", "bar"
redis.strlen("foo")        # => 3
redis.strlen(UUID.v4.to_s) # => 0
Source
tdigest
Source
ts

Return a Redis::TimeSeries that wraps the current Redis::Client, Redis::Cluster, or other Commands-based object.

Source
ttl(key : String)
Source
type(key : String)
Source
wait(numreplicas replica_count : Int | String, timeout : Time::Span)
Source
wait(numreplicas replica_count : Int | String, timeout : Int | String)
Source

Nested types