class Redis
- Redis
- Reference
- Object
Overview
The class is the main entry point for the Redis client.
How to use:
Require the package:
require "redis"
Then instantiate this client class:
redis = Redis.new
Then you can call Redis commands on the redis
object:
redis.set("foo", "bar")
redis.get("foo")
redis.incr("visitors")
See the mixin module Commands for most of the available Redis commands such as #incr, #rename, and so on.
Multithreading / Coroutines
Please mind that a Redis object can't be shared across multiple threads/coroutines! Each thread/coroutine that wants to talk to Redis needs its own Redis object instance.
Included Modules
- Redis::CommandExecution::ValueOriented
- Redis::Commands
Defined in:
redis.crredis/command_execution/future_oriented.cr
redis/command_execution/value_oriented.cr
redis/commands.cr
redis/strategy/base.cr
Constructors
Class Method Summary
-
.open(host = "localhost", port = 6379, unixsocket = nil, password = nil, database = nil, url = nil, ssl = false, ssl_context = nil, dns_timeout = nil, connect_timeout = nil, reconnect = true, command_timeout = nil, namespace : String | Nil = "", &)
Opens a Redis connection, yields the given block with a Redis object and closes the connection.
Instance Method Summary
-
#close
Closes the Redis connection.
-
#multi(&)
Sends Redis commands in transaction mode.
-
#pipelined(&)
Sends Redis commands in pipeline mode.
-
#url
Returns the server URL for this client.
Instance methods inherited from module Redis::CommandExecution::ValueOriented
array_or_nil_command(request : Request) : Array(RedisValue) | Nil
array_or_nil_command,
integer_array_command(request : Request) : Array(RedisValue)
integer_array_command,
integer_command(request : Request) : Int64
integer_command,
integer_or_nil_command(request : Request) : Int64 | Nil
integer_or_nil_command,
string_array_command(request : Request) : Array(RedisValue)
string_array_command,
string_array_or_integer_command(request : Request) : Array(RedisValue) | Int64
string_array_or_integer_command,
string_array_or_string_command(request : Request) : Array(RedisValue) | String
string_array_or_string_command,
string_array_or_string_or_integer_command(request : Request) : Array(RedisValue) | String | Int64
string_array_or_string_or_integer_command,
string_array_or_string_or_nil_command(request : Request) : Array(RedisValue) | String | Nil
string_array_or_string_or_nil_command,
string_command(request : Request) : String
string_command,
string_or_nil_command(request : Request) : String | Nil
string_or_nil_command,
void_command(request : Request) : Nil
void_command
Instance methods inherited from module Redis::Commands
append(key, value)
append,
auth(password)
auth,
bitcount(key, from = nil, to = nil)
bitcount,
bitop(operation, key, keys : Array(String)) : Int64bitop(operation, key, *keys : String) : Int64 bitop, bitpos(key, bit, start = nil, to = nil) bitpos, blpop(keys, timeout_in_seconds) blpop, brpop(keys, timeout_in_seconds) brpop, brpoplpush(source, destination, timeout_in_seconds = nil) brpoplpush, decr(key) decr, decrby(key, decrement) decrby, del(keys : Array)
del(*keys) del, dump(key) dump, echo(message) echo, eval(script : String, keys = [] of RedisValue, args = [] of RedisValue) eval, evalsha(sha1, keys = [] of RedisValue, args = [] of RedisValue) evalsha, exists(key) exists, expire(key, seconds) expire, expireat(key, unix_date) expireat, flushall flushall, flushdb flushdb, geoadd(key, longitude, latitude, place) geoadd, geodist(key, member1, member2, unit = nil) geodist, geohash(key, *args) geohash, geopos(key, *args) geopos, georadius(key, longitude, latitude, radius, unit, withcoord = nil, withdist = nil, withhash = nil, count = nil, sort = nil) georadius, georadiusbymember(key, member, radius, unit, withcoord = nil, withdist = nil, withhash = nil, count = nil, sort = nil) georadiusbymember, get(key) get, getbit(key, index) getbit, getrange(key, start_index, end_index) getrange, getset(key, value) getset, hdel(key, field) hdel, hexists(key, field) hexists, hget(key, field) hget, hgetall(key) hgetall, hincrby(key, field, increment) hincrby, hincrbyfloat(key, field, increment) hincrbyfloat, hkeys(key) hkeys, hlen(key) hlen, hmget(key, fields : Array(String))
hmget(key, *fields : String) hmget, hmset(key, hash) hmset, hscan(key, cursor, match = nil, count = nil) hscan, hset(key, field, value) hset, hsetnx(key, field, value) hsetnx, hvals(key) hvals, incr(key) incr, incrby(key, increment) incrby, incrbyfloat(key, increment) incrbyfloat, info(section : String | Nil = nil) info, keys(pattern) keys, lindex(key, index) lindex, linsert(key, where, pivot, value) linsert, llen(key) llen, lpop(key) lpop, lpush(key, values : Array(RedisValue))
lpush(key, *values) lpush, lpushx(key, value) lpushx, lrange(key, from, to) lrange, lrem(key, count, value) lrem, lset(key, index, value) lset, ltrim(key, start, stop) ltrim, mget(keys : Array(String)) : Array(RedisValue)
mget(*keys) mget, mset(hash : Hash) mset, msetnx(hash) msetnx, object_encoding(key) object_encoding, object_idletime(key) object_idletime, object_refcount(key) object_refcount, persist(key) persist, pexpire(key, milis) pexpire, pexpireat(key, unix_date_in_millis) pexpireat, pfadd(key, values : Array(String)) : Int64
pfadd(key, *values : String) : Int64 pfadd, pfcount(keys : Array(String)) : Int64
pfcount(*keys : String) : Int64 pfcount, pfmerge(keys : Array(String))
pfmerge(*keys : String) pfmerge, ping ping, psetex(key, expire_in_milis, value) psetex, psubscribe(*channel_patterns, &callback_setup_block : Subscription -> )
psubscribe(channel_patterns : Array(String))
psubscribe(*channel_patterns) psubscribe, pttl(key) pttl, publish(channel, message) publish, punsubscribe(channel_patterns : Array(String))
punsubscribe(*channel_patterns) punsubscribe, quit quit, randomkey randomkey, rename(old_key, new_key) rename, renamenx(old_key, new_key) renamenx, restore(key, ttl_in_milis : Int, serialized_value : String, replace = false) restore, rpop(key) rpop, rpoplpush(source, destination) rpoplpush, rpush(key, values : Array(RedisValue))
rpush(key, *values : String) rpush, rpushx(key, value) rpushx, sadd(key, values : Array(RedisValue))
sadd(key, *values) sadd, scan(cursor, match = nil, count = nil) scan, scard(key) scard, script_exists(sha1_array : Array(Reference)) script_exists, script_flush script_flush, script_kill script_kill, script_load(script : String) script_load, sdiff(keys : Array(String)) : Array(RedisValue)
sdiff(*keys : String) : Array(RedisValue) sdiff, sdiffstore(destination, keys : Array(String)) : Int64
sdiffstore(destination, *keys : String) : Int64 sdiffstore, select(database_number : Int32 | Nil) select, set(key, value, ex = nil, px = nil, nx = nil, xx = nil) set, setbit(key, index, value) setbit, setex(key, expire_in_seconds, value) setex, setnx(key, value) setnx, setrange(key, start_index, value) setrange, sinter(keys : Array(String)) : Array(RedisValue)
sinter(*keys : String) : Array(RedisValue) sinter, sinterstore(destination_key, keys : Array(String)) : Int64
sinterstore(destination_key, *keys : String) : Int64 sinterstore, sismember(key, value) sismember, smembers(key) smembers, smove(source, destination, member) smove, sort(key, by = nil, limit = nil, get : Array(RedisValue) | Nil = nil, order = "ASC", alpha = false, store = nil) sort, spop(key, count = nil) spop, srandmember(key, count = nil) srandmember, srem(key, values : Array(RedisValue))
srem(key, *values) srem, sscan(key, cursor, match = nil, count = nil) sscan, strlen(key) strlen, subscribe(*channels, &callback_setup_block : Subscription -> )
subscribe(channels : Array(String)) : Void
subscribe(*channels : String) subscribe, sunion(keys : Array(String))
sunion(*keys : String) sunion, sunionstore(destination, keys : Array(String)) : Int64
sunionstore(destination, *keys : String) : Int64 sunionstore, ttl(key) ttl, type(key) type, unsubscribe(channels : Array(String)) : Nil
unsubscribe(*channels) unsubscribe, unwatch unwatch, watch(keys : Array(String))
watch(*keys) watch, zadd(key, scores_and_members : Array(RedisValue))
zadd(key, *scores_and_members) zadd, zcard(key) zcard, zcount(key, min, max) zcount, zincrby(key, increment, member) zincrby, zinterstore(destination, keys : Array, weights = nil, aggregate = nil) zinterstore, zlexcount(key, min, max) zlexcount, zrange(key, start, stop, with_scores = false) zrange, zrangebylex(key, min, max, limit = nil) zrangebylex, zrangebyscore(key, min, max, limit = nil, with_scores = false) zrangebyscore, zrank(key, member) zrank, zrem(key, member) zrem, zremrangebylex(key, min, max) zremrangebylex, zremrangebyrank(key, start, stop) zremrangebyrank, zremrangebyscore(key, start, stop) zremrangebyscore, zrevrange(key, start, stop, with_scores = false) zrevrange, zrevrangebylex(key, min, max, limit = nil) zrevrangebylex, zrevrangebyscore(key, min, max, limit = nil, with_scores = false) zrevrangebyscore, zrevrank(key, member) zrevrank, zscan(key, cursor, match = nil, count = nil) zscan, zscore(key, member) zscore, zunionstore(destination, keys : Array, weights = nil, aggregate = nil) zunionstore
Constructor Detail
Opens a Redis connection
Options:
- host - the host to connect to
- port - the port to connect to
- unixsocket - instead of using TCP, you can connect to Redis via a Unix domain socket by passing its path here (e.g. "/tmp/redis.sock")
- password - the password for authentication against the server. This is a convenience which saves you the extra call to the Redis
auth
command. - database - the number of the database to select. This a convenience which saves you a call a call to
#select
. - ssl - whether SSL should be enabled.
- ssl_context - a OpenSSL::SSL::Context::Client.
- dns_timeout - the dns timeout.
- connect_timeout - the connect timeout.
- command_timeout - the command timeout - applies when a command takes too long because the Redis-server is blocked by another command or by a dump.
- reconnect - whether we should reconnect when we encounter a disconnected Redis connection.
- url - Redis url. If this is given, it overrides all others.
Example:
redis = Redis.new
redis.incr("counter")
redis.close
Example:
redis = Redis.new(host: "localhost", port: 6379)
...
Example:
redis = Redis.new(unixsocket: "/tmp/redis.sock")
...
Example:
redis = Redis.new(url: "redis://:[email protected]:6380/my-database")
...
Class Method Detail
Opens a Redis connection, yields the given block with a Redis object and closes the connection.
Options:
- host - the host to connect to
- port - the port to connect to
- unixsocket - instead of using TCP, you can connect to Redis via a Unix domain socket by passing its path here (e.g. "/tmp/redis.sock")
- password - the password for authentication against the server. This is a convenience which saves you the extra call to the Redis
auth
command. - database - the number of the database to select. This a convenience which saves you a call a call to
#select
. - ssl - whether SSL should be enabled.
- ssl_context - a OpenSSL::SSL::Context::Client.
- dns_timeout - the dns timeout.
- connect_timeout - the connect timeout.
- command_timeout - the command timeout - applies when a command takes too long because the Redis-server is blocked by another command or by a dump.
- reconnect - whether we should reconnect when we encounter a disconnected Redis connection.
- url - Redis url. If this is given, it overrides all others.
Example:
Redis.open do |redis|
redis.incr("counter")
end
Instance Method Detail
Sends Redis commands in transaction mode.
Yields its block. The block receives as argument an object that has the same API as this class, except:
- it participates in the transaction
- all the Redis commands return Futures
- there is an additional method #discard that will abort the transaction.
Return value: an array with all the responses
- one element for each executed command.
Example:
redis.multi do |multi|
multi.set("foo1", "first")
multi.set("foo2", "second")
end
See the examples repository for more examples.
Sends Redis commands in pipeline mode.
Yields its block. The block receives as argument an object that has the same API as this class, except it participates in pipelining and all Redis commands return Futures.
Return value: an array with all the responses
- one element for each executed command.
Example:
redis.pipelined do |pipeline|
pipeline.set("foo1", "first")
pipeline.set("foo2", "second")
end
See the examples repository for more examples.