diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-20 15:45:45 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-20 15:45:45 +0500 |
| commit | 6898919979c914be4c4650158fe0451a21349bb5 (patch) | |
| tree | 41de46b5fad0bf620f9a6dff3baed4e94914e7aa /backend/src/Redis/Connection.zig | |
| parent | e293a40d6bb62e4fa8cc212fcc8bb4b3501da287 (diff) | |
login
Diffstat (limited to 'backend/src/Redis/Connection.zig')
| -rw-r--r-- | backend/src/Redis/Connection.zig | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/backend/src/Redis/Connection.zig b/backend/src/Redis/Connection.zig index 737d5cb..a69b9ec 100644 --- a/backend/src/Redis/Connection.zig +++ b/backend/src/Redis/Connection.zig @@ -39,10 +39,10 @@ pub fn WriteToTopic(topic: []const u8, message: Message) !void { } } -pub fn ReadFromTopic(topic: []const u8, Key: []const u8) ?[]const u8 { - const raw = redis.redisCommand(connection, "GET %s:%s", - topic.ptr, - Key.ptr); +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; } @@ -53,7 +53,7 @@ pub fn ReadFromTopic(topic: []const u8, Key: []const u8) ?[]const u8 { if (resp.type != redis.REDIS_REPLY_STRING) { return null; } - return std.mem.span(resp.str); + return try allocator.dupe(u8, resp.str[0..resp.len]); } test "Redis connection" { @@ -66,8 +66,18 @@ test "Redis connection" { .SecondsToLive = 1, }); - try std.testing.expectEqualStrings("value", ReadFromTopic("test", "key") orelse "not found"); + const alloc = std.testing.allocator; + + 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 - try std.testing.expectEqualStrings("not found", ReadFromTopic("test", "key") orelse "not found"); + 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); + } } |
