• Stars
    star
    204
  • Rank 192,063 (Top 4 %)
  • Language
    Zig
  • License
    MIT License
  • Created over 5 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Zero-allocation Client for Redis 6+

OkRedis

OkRedis is a zero-allocation client for Redis 6+

Handy and Efficient

OkRedis aims to offer an interface with great ergonomics without compromising on performance or flexibility.

Project status

OkRedis is currently in alpha. The main features are mostly complete, but a lot of polishing is still required. Zig version 0.10.1 is supported. Once async is available in a stable release, OkRedis will pivot to a new Zig version.

Everything mentioned in the docs is already implemented and you can just zig run example.zig to quickly see what it can do. Remember OkRedis only supports Redis 6 and above.

Zero dynamic allocations, unless explicitly wanted

The client has two main interfaces to send commands: send and sendAlloc. Following Zig's mantra of making dynamic allocations explicit, only sendAlloc can allocate dynamic memory, and only does so by using a user-provided allocator.

The way this is achieved is by making good use of RESP3's typed responses and Zig's metaprogramming facilities. The library uses compile-time reflection to specialize down to the parser level, allowing OkRedis to decode whenever possible a reply directly into a function frame, without any intermediate dynamic allocation. If you want more information about Zig's comptime:

By using sendAlloc you can decode replies with arbrirary shape at the cost of occasionally performing dynamic allocations. The interface takes an allocator as input, so the user can setup custom allocation schemes such as arenas.

Quickstart

const std = @import("std");
const okredis = @import("./src/okredis.zig");
const SET = okredis.commands.strings.SET;
const OrErr = okredis.types.OrErr;
const Client = okredis.Client;

pub fn main() !void {
    const addr = try std.net.Address.parseIp4("127.0.0.1", 6379);
    var connection = try std.net.tcpConnectToAddress(addr);

    var client: Client = undefined;
    try client.init(connection);
    defer client.close();

    // Basic interface
    try client.send(void, .{ "SET", "key", "42" });
    const reply = try client.send(i64, .{ "GET", "key" });
    if (reply != 42) @panic("out of towels");


    // Command builder interface
    const cmd = SET.init("key", "43", .NoExpire, .IfAlreadyExisting);
    const otherReply = try client.send(OrErr(void), cmd);
    switch (otherReply) {
        .Nil => @panic("command should not have returned nil"),
        .Err => @panic("command should not have returned an error"),
        .Ok => std.debug.print("success!", .{}),
    }
}

Available Documentation

The reference documentation is available here.

Extending OkRedis

If you are a Lua script or Redis module author, you might be interested in reading the final sections of COMMANDS.md and REPLIES.md.

Embedding OkRedis in a higher level language

Take a look at the final section of REPLIES.md.

TODOS

  • Implement remaining command builders
  • Better connection management (ipv6, unixsockets, ...)
  • Streamline design of Zig errors
  • Refine support for async/await and think about connection pooling
  • Refine the Redis traits
  • Pub/Sub

More Repositories

1

zine

Fast, Scalable, Flexible Static Site Generator (SSG)
Zig
421
star
2

bork

A TUI chat client tailored for livecoding on Twitch.
Zig
265
star
3

redis-cuckoofilter

Hashing-function agnostic Cuckoo filters for Redis
Zig
230
star
4

superhtml

HTML Language Server & Templating Language Library
Zig
165
star
5

ziggy

A data serialization language for expressing clear API messages, config files, etc.
Zig
148
star
6

zig-cuckoofilter

Production-ready Cuckoo Filters for any C ABI compatible target.
Zig
82
star
7

redis-memolock

Redis MemoLock - Distributed Caching with Promises
C#
76
star
8

zig-doctest

A tool for testing snippets of code, useful for websites and books that talk about Zig.
Zig
76
star
9

zig-showtime

See you at SHOWTIME!
HTML
29
star
10

ZigAdventures

Zig Adventures on iOS
Zig
22
star
11

simplex

CLI tool that lets you post on Twitter, Mastodon, Bluesky and LinkedIn with a single command.
Shell
21
star
12

zig-afl-kit

Convenience functions for easy integration with AFL++ for both Zig and C/C++ programmers!
Zig
21
star
13

allyourcodebase.com

CSS
20
star
14

art

Art related to Zig
15
star
15

zig-live-webserver

A webserver that supports hot reloading and that integrates with the Zig build system
Zig
8
star
16

suzie

Suzie is the bot that promotes Zig livecoding streams in some Discord servers
Zig
7
star
17

zig-spoon

Zig
7
star
18

vcf-mongo

Ruby
6
star
19

formazione-sicurezza-clicker

Click! Hehe
6
star
20

zine-ssg.io

Official website for https://github.com/kristoff-it/zine
CSS
6
star
21

supermd

SuperMD is an extension of Markdown used by https://zine-ssg.io
Zig
5
star
22

zig-rax

Zig radix tree implementation based on antirez/rax
C
4
star
23

aoc21

yo
Zig
4
star
24

zig-redismodule

Redis modules for Zig
Zig
4
star
25

advanced-hello-world

Become and EXTREME Zig programmer!
C
3
star
26

ziggy-lang.io

Website for https://github.com/kristoff-it/ziggy
CSS
3
star
27

redis-moonkey

A Redis Module that executes string keys as Lua scripts.
C
2
star
28

redis-setne

Quick implementation of a SET command that doesn't generate spurious keyspace events.
C
2
star
29

zenlang-ziglang-eventloop

Comparison between Zen and Zig event loop source code.
2
star
30

simple-websocket-chat

Simple websocket chat demo that uses Redis Pub/Sub for horizontal scaling
JavaScript
2
star
31

kristoff.it

CSS
2
star
32

buzz-repro

Zig
2
star
33

scripty

The perfect scripting sidekick!
Zig
2
star
34

orca-ui-hello-zig

A incomplete port of Orca's samples/UI example using Zig
Zig
1
star
35

GSoC-2014-OBF

Repository containing the blog and other non-code products of my GSoC participation.
Python
1
star
36

zig.show

HTML
1
star
37

customs-news-crawler

Collection of Scrapy spiders used to crawl the customs' websites of some east-european nations.
Python
1
star
38

mida-crawl

Instert keywords, obtain data from http://mida.ansa.it, from 'ALL TIME', containing ALL keywords. Wildcards allowed.
1
star
39

softwareyoucan.love

HTML
1
star
40

redis-client-python-scratch

Code from my writing a Redis client from scratch session
Python
1
star
41

emm-search-tobacco

Script used to extract as CSV all the results of multiple searches performed on http://emm.newsbrief.eu
Python
1
star