db/redis
lib/db/redis.vklib/db/redis.vk
Pure-Veko RESP3 / RESP2 wire protocol codec and client over io/Stream.
Supports simple strings (+), errors (-), integers (:), bulk strings ($),
arrays (*), RESP3 null (_), booleans (#), doubles (,), and maps (%),
plus command helpers (ping, get, set, del, hset, hget, pipeline).
DecodedResp = struct { value any, consumed i64 }
Decoded RESP value along with the number of bytes consumed from the wire buffer.
RedisClient = struct { stream io/Stream, rx_buf bytes, transport_kind enum = ... }
Connected Redis client wrapping an io/Stream and read buffer.
RedisConfig = struct { host str = ..., port i64 = ..., username str = ..., password str = ..., db i64 = ..., tls bool = ..., tls_opts map = ..., resp3 bool = ..., connect_timeout dur = ..., io_timeout dur = ... }
Connection, TLS 1.3, ACL authentication, and RESP3 negotiation options for connect and new-pool.
RedisPool = struct { cfg RedisConfig, max_conns i64, acquire_timeout dur = ..., max_idle_ns i64 = ..., health_check_ns i64 = ..., sem_ch any, idle_ch any }
Thread-safe channel-backed Redis connection pool (safe to share across spawn workers).
encode-command = fn [args block] bytes
Encodes a Redis command (block of strings) as a RESP array of bulk strings (*N\r\n$L\r\n...).
use [redis = db/redis]
wire = redis/encode-command ['SET' 'k1' 'hello']
if (bytes/to-str wire) != '*3\r\n$3\r\nSET\r\n$2\r\nk1\r\n$5\r\nhello\r\n' [ err 'resp enc' ] else [ .ok ]
encode-value = fn [v any] bytes
Encodes a Veko value into RESP3 wire bytes (useful for both clients and mock/embedded RESP3 servers).
decode-bytes = fn [raw bytes] DecodedResp
Decodes one RESP3 / RESP2 value from raw bytes.
use [redis = db/redis]
wire = redis/encode-value { status = 'ok', count = 7, flag = true }
dec = redis/decode-bytes wire
if (map/get dec.value 'status') != 'ok' or (map/get dec.value 'count') != 7 or (map/get dec.value 'flag') != true [
err 'resp3 map'
] else [ .ok ]
from-stream = fn [stream io/Stream] RedisClient
Wraps an io/Stream (TCP, TLS 1.3, or memory) in a RedisClient.
cmd = fn [mut cli RedisClient, args block] any
Sends a single Redis command and returns the decoded RESP3/RESP2 reply (rejecting .secret leakage).
pipeline = fn [mut cli RedisClient, cmds block[block]] block
Pipelines multiple commands in a single write and reads all replies in order.
set-ex = fn [mut cli RedisClient, key str, val str, ttl_sec i64] any
Sends SET key val EX ttl_sec.
connect = fn [cfg RedisConfig] RedisClient
Connects to Redis over TCP or TLS 1.3 (cfg.tls == true), performing optional ACL AUTH, HELLO 3, and SELECT db.
with-tx = fn [mut cli RedisClient, work_fn any] any
Executes (work_fn mut cli) inside a MULTI / EXEC transaction (sending DISCARD if work_fn returns err).
new-pool = fn [cfg RedisConfig, /max max_conns i64] RedisPool
Creates a thread-safe RedisPool bounded to max_conns concurrent connections (safe across spawn workers).
with-conn = fn [pool RedisPool, work_fn any] any
Borrows a healthy RedisClient from pool (probing PING -> 'PONG' & auto-reconnecting on stale sockets), runs (work_fn mut cli), and returns it.