summaryrefslogtreecommitdiff
path: root/backend/src/Redis/Connection.zig
blob: 360ae448a1ce8ab419d4d6fa64699ca74cb60686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const std = @import("std");
const settings = @import("../Settings.zig");
const redis = @cImport(
    @cInclude("hiredis/hiredis.h")
    );

var connection: ?*redis.redisContext = undefined;
const addr: [*]const u8 = "127.0.0.1";
const port: u16 = 6379;

pub fn Connect() !void {
    connection = redis.redisConnect(
        settings.Current.RedisParams.Address.ptr,
        settings.Current.RedisParams.Port);

    if (connection == null or connection.?.err != 0) {
        if (connection != null) {
            @panic(&connection.?.errstr);
        }
        @panic("The redis connection failed: general error");
    }
}

pub fn Disconnect() void {
    redis.redisFree(connection);
}

pub const Message = struct {
    Key: []const u8,
    Value: []const u8,
    SecondsToLive: u16
};

pub fn WriteToTopic(topic: []const u8, message: Message) !void {
    const rep = redis.redisCommand(connection, "SET %b:%b %b EX %d", 
        topic.ptr, topic.len,
        message.Key.ptr, message.Key.len,
        message.Value.ptr, message.Value.len,
        message.SecondsToLive); 

    if (rep != null) {
        redis.freeReplyObject(rep);
    }
}

pub fn ReadFromTopic(allocator: std.mem.Allocator, topic: []const u8, Key: []const u8) !?[]const u8 {
    const raw = redis.redisCommand(connection, "GET %b:%b", 
            topic.ptr, topic.len,
            Key.ptr, Key.len);
    if (raw == null) {
        return null;
    }
    defer {
        redis.freeReplyObject(raw);
    }
    const resp: *redis.redisReply = @alignCast(@ptrCast(raw));
    if (resp.type != redis.REDIS_REPLY_STRING) {
        return null;
    }
    return try allocator.dupe(u8, resp.str[0..resp.len]);
}

test "Redis connection" {
    const alloc = std.testing.allocator;

    try settings.ReadConfig(alloc);
    defer settings.Deinit(alloc);

    try Connect();
    defer Disconnect();

    try WriteToTopic("test", Message {
        .Key = "key",
        .Value = "value",
        .SecondsToLive = 1,
    });


    var resp = try ReadFromTopic(alloc, "test", "key");
    try std.testing.expectEqualStrings("value", resp orelse "not found");
    std.Thread.sleep(1_500_000_000);
    // KVP TTL check
    if (resp) |r|  {
        alloc.free(r);
    }
    resp = try ReadFromTopic(alloc, "test", "key");
    try std.testing.expectEqualStrings("not found", resp orelse "not found");
    if (resp) |r|  {
        defer alloc.free(r);
    }
}