A Redis client library for Zig, built on zio for async I/O.
- Async I/O via zio coroutines
- Connection pooling
- RESP2 protocol implementation
- Basic string commands (GET, SET, DEL, INCR, DECR, etc.)
- TTL and expiration support
- Retry logic with configurable attempts and intervals
const std = @import("std");
const zio = @import("zio");
const redis = @import("redis");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var rt = try zio.Runtime.init(gpa.allocator(), .{});
defer rt.deinit();
var client = try redis.connect(gpa.allocator(), "localhost:6379");
defer client.deinit();
// Set a value
try client.set("hello", "world", .{});
// Get a value
var buf: [1024]u8 = undefined;
if (try client.get("hello", &buf)) |value| {
std.debug.print("Value: {s}\n", .{value});
}
// Set with expiration
try client.set("temp", "data", .{ .ex = 60 });
// Increment a counter
try client.set("counter", "0", .{});
const val = try client.incrBy("counter", 5);
std.debug.print("Counter: {d}\n", .{val});
}get(key, buf)- Get the value of a keyset(key, value, opts)- Set the string value of a key- Options:
ex(expire seconds),nx(only if not exists),xx(only if exists),get(return old value)
- Options:
del(keys)- Delete one or more keysincr(key)- Increment the integer value of a key by oneincrBy(key, delta)- Increment the integer value by amountdecr(key)- Decrement the integer value of a key by onedecrBy(key, delta)- Decrement the integer value by amountexpire(key, seconds)- Set a timeout on keyttl(key)- Get the time to live for a keyexists(keys)- Determine if keys exist
ping(message)- Ping the serverflushDB()- Remove all keys from the current databasedbSize()- Return the number of keys in the current database
Add redis.zig as a dependency in your build.zig.zon:
zig fetch --save "git+https://github.com/lalinsky/redis.zig"In your build.zig:
const redis = b.dependency("redis", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("redis", redis.module("redis"));Run tests:
zig build testRun example:
docker compose up -d
zig build run- Pipelining support
- Pub/Sub support
- Transaction support (MULTI/EXEC)
- Data structure commands (lists, sets, hashes)
- Lua scripting support
- Cluster support
MIT