Connecting to Redis
You can connect to Redis using theRedis
type. You should register this type for injection in your Application.boot()
. It conforms to Service
so you can do so with the config
function.
Clusters
If you’re using a Redis cluster, your client can connect to multiple instances by passing multipleSocket
s to the initializer. Connections will be distributed across the instances.
Interacting With Redis
Redis
conforms to RediStack.RedisClient
meaning that by default, it has functions around nearly all Redis commands.
You can easily get and set a value.
command
. The first argument is the command name, all subsequent arguments are the command’s arguments.
Scripting
You can run a script via.eval(...)
.
Scripts are written in Lua and have access to 1-based arrays KEYS
and ARGV
for accessing keys and arguments respectively. They also have access to a redis
variable for calling Redis inside the script. Consult the EVAL documentation for more information on scripting.
Pub / Sub
Redis providespublish
and subscribe
commands to publish and listen to various channels.
You can easily subscribe to a single channel or multiple channels.
unsubscribe
.
Wildcard Subscriptions
You may subscribe to wildcard channels usingpsubscribe
.
punsubscribe
.
Transactions
Sometimes, you’ll want to run multiple commands atomically to avoid race conditions. Alchemy makes this simple with thetransaction()
function which provides a wrapper around Redis’ native MULTI
& EXEC
commands.