A lightweight Redis client, implemented in Julia.
Links to detailed interfaces and documentation:
- Basic command execution
- Executing commands with a global client instance
- Pipelining
- Transactions
- Pub/Sub
- Redis locks
- Support for secured Redis connection (SSL/TLS)
Establishing a basic client connection:
client = Client(host="localhost", port=6379)
Establishing a secured client (SSL/TLS) connection:
ssl_config = get_ssl_config(ssl_certfile="redis.crt", ssl_keyfile="redis.key", ssl_ca_certs="ca.crt")
client = Client(ssl_config=ssl_config)
Setting and getting the global client:
set_global_client(client)
get_global_client()
Executing commands:
set("key", "value"; client=client)
get("key") # uses global client by default
execute(["DEL", "key"], client) # custom commands
Using pipelining to speed up queries:
# Normal
pipe = Pipeline()
set("key", "value"; client=pipe)
get("key"; client=pipe)
results = execute(pipe)
# Do-block
results = pipeline() do pipe
lpush("example", 1, 2, 3, 4; client=pipe)
lpop("example"; client=pipe)
rpop("example"; client=pipe)
lpop("example"; client=pipe)
end
Executing a group of commands atomically with MULTI/EXEC transactions:
# Normal
multi()
set("key", "value")
get("key")
results = exec()
# Do-block
results = multi_exec() do
set("key", "value")
get("key")
get("key")
end
Executing a MULTI/EXEC transaction within a pipeline:
results = pipeline() do pipe
lpush("example", 1, 2, 3, 4; client=pipe)
lpop("example"; client=pipe)
rpop("example"; client=pipe)
multi_exec(; client=pipe) do
lpop("example"; client=pipe)
rpop("example"; client=pipe)
end
lpop("example"; client=pipe)
end
Using Redis Pub/Sub (interfaces for subscribe
and psubscribe
are the same):
# Set up channels, publisher and subscriber clients
channels = ["first", "second"]
publisher = Client()
subscriber = Client()
# Begin the subscription
stop_fn(msg) = msg[end] == "close subscription"; # stop the subscription loop if the message matches
messages = []
@async subscribe(channels...; stop_fn=stop_fn, client=subscriber) do msg
push!(messages, msg)
end # Without @async this function will block, alternatively use Thread.@spawn
wait_until_subscribed(subscriber)
subscriber.is_subscribed # outputs true
subscriber.subscriptions # set of actively subscribed channels
# Publish to channels
publish("first", "hello"; client=publisher)
publish("second", "world"; client=publisher)
# Unsubscribing
unsubscribe("first"; client=subscriber)
wait_until_channel_unsubscribed(subscriber, "first")
subscriber.subscriptions
unsubscribe(; client=subscriber) # unsubscribe from all channels
wait_until_unsubscribed(subscriber)
subscriber.is_subscribed # outputs false
subscriber.subscriptions # set of actively subscribed channels should be empty
Using redis locks for performing atomic operations:
@async redis_lock("example_lock") do
sleep(3) # Lock will exist for 3 seconds
end
while !isredislocked("example_lock")
sleep(0.1) # Ensure async lock is active before proceeding
end
redis_lock("example_lock") do
println("This message will be delayed by 3 seconds!") # Blocked by first lock
end