class

Redis::Client

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

The Redis client is the expected entrypoint for this shard. By default, it will connect to localhost:6379, but you can also supply a URI to connect to an arbitrary Redis server. SSL, password authentication, and DB selection are all supported.

# Connects to localhost:6379
redis = Redis::Client.new

# Connects to a server at "redis.example.com" on port 6000 over a TLS
# connection, authenticates with the password "password", and uses DB 3
redis = Redis::Client.new(URI.parse("rediss://:password@redis.example.com:6000/3"))

# Connects to a server at the URL in `ENV["REDIS_URL"]`
redis = Redis::Client.from_env("REDIS_URL")

Constructors

new(uri : URI = URI.parse(ENV.fetch("REDIS_URL", "redis:///")), log : ::Log = Log)

The client holds a pool of connections that expands and contracts as needed.

Source

Class methods

from_env(env_var)
Source

Instance methods

close
Source
hscan_each(key : String, *, match pattern : String | Nil = nil, count : String | Int | Nil = nil, &) : Nil
Source
multi
Source
pipeline
Source
psubscribe(*channels, &)
Source
run(command)

All Redis commands invoked on the client check out a connection from the connection pool, invoke the command on that connection, and then check the connection back into the pool.

redis = Redis::Client.new
Source
scan_each(match pattern : String | Nil = nil, count : String | Int | Nil = nil, type : String | Nil = nil, &) : Nil
Source
sscan_each(key : String, *, match pattern : String | Nil = nil, count : String | Int | Nil = nil, &) : Nil
Source
sscan_each(key : String, *, match pattern : String | Nil = nil, count : String | Int | Nil = nil)
Source
subscribe(*channels, &)
Source
watch(*keys : String, &)

Watch the given keys for changes when you need to fetch them for update in a transaction and yield the connection with that watch active. This allows for optimistic updates within the transaction, returning nil from Transaction#exec if any of the keys are modified before the transaction completes.

# Begin watching the `session:123` key, yields the connection that's
# watching it
redis.watch "session:123" do |conn|
  session = Session.from_json(conn.get!("session:123"))
  session.user_id = user.id

  # Begin a new tra
  conn.multi do |txn|
    txn.set "session:123", session.to_json
  end
end

NOTE: This does not prevent concurrent updates the way an RDBMS like Postgres does. Redis has no way to prevent that when performing more than a single command. However, this pattern allows you to detect a concurrent update (the multi block returns nil), so you'll need to design your interactions with the Redis server around this and, if necessary, retry the transaction according to your application's needs. IMPORTANT: Use this sparingly. Whenever feasible, instead of a fetch/mutate/save cycle, update keys atomically in Redis. For example, if you're using a JSON field, set properties directly with JSON#set or increment them with JSON#numincrby. Redis is designed specifically to work that way.

Source
zscan_each(key : String, *, match pattern : String | Nil = nil, count : String | Int | Nil = nil, &) : Nil
Source

Nested types