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
Included Modules
- Redis::Commands::Geo
- Redis::Commands::Hash
- Redis::Commands::HyperLogLog
- Redis::Commands::List
- Redis::Commands::Set
- Redis::Commands::SortedSet
- Redis::Commands::Stream
Direct including types
- Redis::Client
- Redis::Cluster
- Redis::Connection
- Redis::Pipeline
- Redis::ReplicationClient
- Redis::Transaction
Defined in:
commands.crcommands/hash.cr
graph.cr
json.cr
search.cr
time_series.cr
Instance Method Summary
-
#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 : Enumerable(String))
Delete all specified keys and return the number of keys deleted.
-
#del(*keys : String)
Delete all specified keys and return the number of keys deleted.
- #eval(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)
- #eval_ro(script : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)
- #evalsha(sha : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)
- #evalsha_ro(sha : String, keys : Enumerable(String) = EmptyEnumerable.new, args : Enumerable(String) = EmptyEnumerable.new)
-
#exists(keys : Enumerable(String))
Return the number of specified keys that exist
-
#exists(*keys : String)
Return the number of specified keys that exist
- #expire(key : String, ttl : Time::Span)
- #expire(key : String, ttl : Int)
- #expireat(key : String, at : Time)
-
#flushall
Delete all the keys of all the existing databases, not just the currently selected one.
-
#flushdb
Delete all the keys of the currently selected DB
-
#ft
EXPERIMENTAL RediSearch support is still under development. Some APIs may change while details are discovered.
-
#get(key : String)
Get the value for the specified key
-
#getdel(key : String)
Delete
keyand return its value, similar toHash#deletein Crystal. -
#graph(key : String)
Instantiate a
Redis::Graph::Clientbacked by thisRedis::Client. -
#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
-
#incrbyfloat(key : String, amount : Float | String)
Atomically increment and return the floating-point value for
keybyamount, creating it as if it were0if it does not exist. - #info
-
#json
Return a
Redis::JSONinstance that wraps the currentRedis::ClientorRedis::Cluster.EXPERIMENTAL Support for the RedisJSON module is still under development and subject to change.
-
#keys(pattern = "*")
Get the keys whose names follow the specified glob pattern.
- #mget(keys : Enumerable(String))
- #mset(data : Hash(String, String))
- #pexpire(key : String, ttl : Time::Span)
- #pexpire(key : String, ttl : Int)
- #pexpireat(key : String, at : Time)
-
#ping(message : String | Nil = nil)
Send a
PINGcommand to the server, returningmessageif it is provided or"PONG"otherwise. - #pttl(key : String)
- #publish(channel : String, message : String)
-
#run(command)
Execute the given command and return the result from the server.
- #scan(cursor : String = "0", match : String | Nil = nil, count : String | Int | Nil = nil, type : String | Nil = nil)
-
#script_exists(shas : Enumerable(String))
Return an array where each entry is
1if the corresponding entry in the list ofshasexists or0if it does not. -
#script_exists(*shas : String)
Return an array where each entry is
1if the corresponding entry in the list ofshasexists or0if it does not. -
#script_flush(mode : ScriptFlushMode)
Flush the Lua scripts cache, see the official docs.
-
#script_kill
Kill the currently executing
#evalscript, assuming no write operation was yet performed by the script. -
#script_load(script : String)
Preload a Lua script, returning the SHA of the script to pass to
#evalsha. -
#set(key, value, *, ex : Time, nx = false, xx = false, keepttl = false, get = false)
Sets the value stored at
keytovalue, expiring at the givenTimewith millisecond precision. -
#set(key, value, *, ex : Time::Span, nx = false, xx = false, keepttl = false, get = false)
Sets the value stored at
keytovalue, expiring after the givenTime::Spanwith millisecond precision. -
#set(key : String, value : String, *, ex : String | Int | Nil = nil, px : String | Int | Nil = nil, nx = false, xx = false, keepttl = false, get = false)
Set the value stored at
keytovalue, optionally specifying expiration. -
#ts
Return a
Redis::TimeSeriesthat wraps the currentRedis::Client,Redis::Cluster, or otherCommands-based object. - #ttl(key : String)
- #type(key : String)
- #unlink(keys : Enumerable(String))
- #unlink(*keys : String)
- #wait(numreplicas replica_count : Int | String, timeout : Time::Span)
- #wait(numreplicas replica_count : Int | String, timeout : Int | String)
Instance methods inherited from module Redis::Commands::HyperLogLog
pfadd(key : String, values : Enumerable(String))pfadd(key : String, *values : String) pfadd, pfcount(keys : Enumerable(String))
pfcount(*keys : String) pfcount, pfmerge(destination_key target : String, source_keys sources : Enumerable(String))
pfmerge(destination_key target : String, *source_keys : String) pfmerge
Instance methods inherited from module Redis::Commands::Geo
geoadd(key : String, *entries : String, nx = nil, xx = nil, ch = nil)
geoadd,
geopos(key : String, *members : String)
geopos,
geosearch(key : String, *, fromlonlat lonlat : Tuple(String, String), byradius radius : Redis::Geo::Radius, sort : Redis::Geo::Sort | Nil = nil, count : Int | String | Nil = nil, withcoord : Bool = false, withdist : Bool = false)
geosearch
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, fields : NamedTuple | ::Hash(String, String))
xadd(key : String, id : String, *, maxlen, fields : NamedTuple | ::Hash(String, String))
xadd(key : String, id : String, *, minid, fields : NamedTuple | ::Hash(String, String))
xadd(key : String, id : String, *, maxlen : Tuple(String, String) | Nil, minid : Tuple(String, String) | Nil, fields : NamedTuple | Hash(String, String))
xadd(key : String, id : String, **fields : String)
xadd(key : String, id : String, *, maxlen, **fields : String)
xadd(key : String, id : String, *, minid, **fields : String) 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, xtrim(key : String, minid : Tuple(String, String), limit : String | Int | Nil = nil) xtrim
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, zscan(key : String, cursor : String, match pattern : String | Nil = nil, count : String | Nil = nil) zscan, zscore(key : String, value : String) zscore
Instance methods inherited from module Redis::Commands::Set
sadd(key : String, values : Enumerable(String))sadd(key : String, *values : String) sadd, scard(key : String) scard, sdiff(first : String, second : String) sdiff, sinter(keys : Enumerable(String))
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, sscan(key : String, cursor : String, match pattern : String | Nil = nil, count : String | Nil = nil) sscan
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
Instance Method Detail
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
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
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"])
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"])
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"])
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"])
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
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
Delete all the keys of all the existing databases, not just the currently selected one.
EXPERIMENTAL RediSearch support is still under development. Some APIs may change while details are discovered.
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
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.
Return a Redis::JSON instance that wraps the current Redis::Client or
Redis::Cluster.
EXPERIMENTAL Support for the RedisJSON module is still under development and subject to change.
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"]
Send a PING command to the server, returning message if it is provided
or "PONG" otherwise.
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"})
Return an array where each entry is 1 if the corresponding entry in the
list of shas exists or 0 if it does not.
Return an array where each entry is 1 if the corresponding entry in the
list of shas exists or 0 if it does not.
Flush the Lua scripts cache, see the official docs.
redis.script_flush :async # Flush scripts asynchronously
redis.script_flush :sync # Flush scripts immediately
Kill the currently executing #eval script, assuming no write operation
was yet performed by the script.
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"
Sets the value stored at key to value, expiring at the given Time with millisecond precision.
Sets the value stored at key to value, expiring after the given Time::Span with millisecond precision.
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 inkey, providing the functionality inGETSETex: TTL in seconds (mnemonic: "ex" = "expiration")px: TTL in millisecondskeepttl: 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", "value", nx: true # => "OK"
redis.set "foo", "other-value", 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.
Return a Redis::TimeSeries that wraps the current Redis::Client,
Redis::Cluster, or other Commands-based object.