veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

db/redis

lib/db/redis.vk

lib/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).

signature
DecodedResp = struct { value any, consumed i64 }

Decoded RESP value along with the number of bytes consumed from the wire buffer.

signature
RedisClient = struct { stream io/Stream, rx_buf bytes, transport_kind enum = ... }

Connected Redis client wrapping an io/Stream and read buffer.

signature
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
signature
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).

signature
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...).

Executable Example
veko
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 ]
signature
encode-value = fn [v any] bytes

Encodes a Veko value into RESP3 wire bytes (useful for both clients and mock/embedded RESP3 servers).

signature
decode-bytes = fn [raw bytes] DecodedResp

Decodes one RESP3 / RESP2 value from raw bytes.

Executable Example
veko
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 ]
signature
from-stream = fn [stream io/Stream] RedisClient

Wraps an io/Stream (TCP, TLS 1.3, or memory) in a RedisClient.

cmd fn
signature
cmd = fn [mut cli RedisClient, args block] any

Sends a single Redis command and returns the decoded RESP3/RESP2 reply (rejecting .secret leakage).

ping fn
signature
ping = fn [mut cli RedisClient] any

Sends PING (returns 'PONG').

get fn
signature
get = fn [mut cli RedisClient, key str] any

Sends GET key (returns string or .null).

set fn
signature
set = fn [mut cli RedisClient, key str, val str] any

Sends SET key val.

del fn
signature
del = fn [mut cli RedisClient, key str] any

Sends DEL key.

hset fn
signature
hset = fn [mut cli RedisClient, key str, field str, val str] any

Sends HSET key field val.

hget fn
signature
hget = fn [mut cli RedisClient, key str, field str] any

Sends HGET key field.

signature
pipeline = fn [mut cli RedisClient, cmds block[block]] block

Pipelines multiple commands in a single write and reads all replies in order.

signature
close = fn [mut cli RedisClient] enum

Closes the underlying io/Stream.

signature
set-ex = fn [mut cli RedisClient, key str, val str, ttl_sec i64] any

Sends SET key val EX ttl_sec.

incr fn
signature
incr = fn [mut cli RedisClient, key str] any

Sends INCR key (returns incremented i64).

signature
expire = fn [mut cli RedisClient, key str, ttl_sec i64] any

Sends EXPIRE key ttl_sec.

connect fn caps: .net
signature
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.

signature
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).

signature
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 caps: .clocks
signature
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.

signature
close-pool = fn [pool RedisPool] i64

Drains and closes all idle connections in pool.