• Stars
    star
    192
  • Rank 195,147 (Top 4 %)
  • Language
    Zig
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 year 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

redis-cuckoofilter

Hashing-function agnostic Cuckoo filters for Redis
Zig
225
star
2

bork

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

zine

Fast, Scalable, Flexible Static Site Generator (SSG)
Zig
176
star
4

zig-cuckoofilter

Production-ready Cuckoo Filters for any C ABI compatible target.
Zig
78
star
5

redis-memolock

Redis MemoLock - Distributed Caching with Promises
C#
75
star
6

zig-doctest

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

ziggy

A Zig-flavored data format.
Zig
56
star
8

zig-showtime

See you at SHOWTIME!
HTML
29
star
9

ZigAdventures

Zig Adventures on iOS
Zig
20
star
10

simplex

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

allyourcodebase.com

CSS
19
star
12

art

Art related to Zig
14
star
13

suzie

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

zig-spoon

Zig
7
star
15

vcf-mongo

Ruby
6
star
16

formazione-sicurezza-clicker

Click! Hehe
6
star
17

aoc21

yo
Zig
4
star
18

zig-redismodule

Redis modules for Zig
Zig
3
star
19

redis-moonkey

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

redis-setne

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

zig-rax

Zig radix tree implementation based on antirez/rax
C
2
star
22

zenlang-ziglang-eventloop

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

simple-websocket-chat

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

advanced-hello-world

Become and EXTREME Zig programmer!
C
2
star
25

kristoff.it

CSS
2
star
26

zine-ssg.io

Official website for https://github.com/kristoff-it/zine
HTML
2
star
27

ziggy-website

2
star
28

orca-ui-hello-zig

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

GSoC-2014-OBF

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

zig.show

HTML
1
star
31

customs-news-crawler

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

mida-crawl

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

redis-client-python-scratch

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

softwareyoucan.love

HTML
1
star
35

emm-search-tobacco

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