A memcached client library for Zig, built on zio for async I/O. Uses the modern meta protocol for efficient communication.
- Async I/O via zio coroutines
- Connection pooling per server
- Multi-server support with consistent hashing (rendezvous)
- Meta protocol (mg, ms, md, ma commands)
- CAS (compare-and-swap) support
- TTL and flags support
const std = @import("std");
const zio = @import("zio");
const memcached = @import("memcached");
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 memcached.connect(gpa.allocator(), "localhost:11211");
defer client.deinit();
// Set a value
try client.set("hello", "world", .{ .ttl = 300 });
// Get a value
var buf: [1024]u8 = undefined;
if (try client.get("hello", &buf, .{})) |info| {
std.debug.print("Value: {s}\n", .{info.value});
}
// Increment a counter
try client.set("counter", "0", .{});
const val = try client.incr("counter", 1);
std.debug.print("Counter: {d}\n", .{val});
}var client = try memcached.Client.init(gpa.allocator(), .{
.servers = &.{
"server1:11211",
"server2:11211",
"server3:11211",
},
.hasher = .rendezvous,
});
defer client.deinit();Add memcached.zig as a dependency in your build.zig.zon:
zig fetch --save "git+https://github.com/lalinsky/memcached.zig"In your build.zig:
const memcached = b.dependency("memcached", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("memcached", memcached.module("memcached"));MIT