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
Return a BloomFilter, allowing you to run commands for Bloom Filters.
redis.bf.add "my-bloom-filter", "value"
Send a DBSIZE command to the server, returning the number of keys in the
currently-selected database.
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.
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", "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.
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
Return a Redis::TimeSeries that wraps the current Redis::Client,
Redis::Cluster, or other Commands-based object.