A Redis client for Julia, built on Reseau.jl.
Commands are pipelined over a single connection: each command is written under a lock and a future is queued for its reply, so concurrent callers share one socket without blocking each other.
using Redis
client = Redis.connect("127.0.0.1", 6379)
Redis.set(client, "key", "value")
Redis.get(client, "key") # "value"
Redis.del(client, "key")
# expiry
Redis.set(client, "session", "abc"; ex=60) # seconds
Redis.set(client, "otp", "123456"; px=30_000) # milliseconds
# only set if absent
Redis.set(client, "lock", "owner"; nx=true)
# iterate keys matching a glob
for k in Redis.Scan(client, "session:*")
@show k
end
# pipeline a batch
batch = Redis.Batch()
push!(batch, Redis.Commands.set("a", "1"))
push!(batch, Redis.Commands.get("a"))
Redis.execute(client, batch)
# a transaction in one call: queued commands reply "QUEUED", EXEC returns their results
C = Redis.Commands
Redis.execute(client, C.multi(), C.incrby("n", 1), C.get("n"), C.exec()) # ["OK", "QUEUED", "QUEUED", Any[1, "1"]]
close(client)MULTI is state of the connection, so a client shared by several tasks should send
each transaction in one execute call as above. Sent one command at a time
(Redis.multi(client), commands, Redis.exec(client)), a command whose result is
not a string is refused with an ArgumentError, because the server only answers
QUEUED. If the connection drops inside MULTI, the server discards the
transaction and the next command throws RedisConnectionError instead of
reconnecting and running outside it. Redis.exec returns nothing when a
WATCHed key changed.
Sorted sets, streams and consumer groups, sets, hashes, lists, geo commands, and
transactions are supported; see src/commands.jl for the full surface.
Tests run against a real Redis in a throwaway container via Harbor.jl, so Docker is required.
julia --project -e 'using Pkg; Pkg.test()'