cdt.redis
Zero-dependency RESP2 TCP Redis client with string, JSON, Hash, List, Set, raw command, and Lua-verified distributed lock APIs.
conduit
import * as redis from "cdt.redis"
struct JobPayload {
id: Int
kind: String
}
func main() -> Void | redis.RedisError {
r := redis.connect("redis://127.0.0.1:6379/0")?
r.ping()?
r.set_json("job:1", JobPayload(id: 1, kind: "email"), 300)?
loaded := r.get_json<JobPayload>("job:1")?
n := r.with_lock("lock:job:1", 5000, () => {
return r.incr("jobs:processed")
})?
r.close()
}Connection URL Format
redis://[:password@]host[:port][/db][?timeout_ms=5000]rediss://[:password@]host[:port][/db][?tls_verify=full|ca|none&ca_file=/path/to/ca.pem]redis://username:password@host:6379/1(ACLAUTH user pass+SELECT 1)- Non-loopback connections using plaintext
redis://ortls_verify=noneemit a one-time warning per host; userediss://(defaulttls_verify=full, ortls_verify=ca&ca_file=...for internal CAs) in non-local environments (Conduit 0.4.0 will enforce verified TLS by default for non-loopback hosts).
Types
Client— Thread-safe Redis connection handle.Lock { key: String, token: String }— Distributed lock handle acquired viaClient.lock(key, ttl_ms).l.unlock() -> Bool | RedisError— Safely unlocks only if the stored value still equalstoken.l.extend(ttl_ms: Int) -> Bool | RedisError— Refreshes TTL only if still owner.
RedisError { kind: String, message: String }— Error kinds:"invalid_url","connect","auth","io","protocol","redis_error","encode","decode","invalid_input","lock_busy","closed".
Module Functions
redis.connect(url: String) -> Client | RedisError
Client Methods
Connection & Raw
ping() -> String | RedisErrorcmd(args: [String]) -> String? | RedisError— Sends any raw RESP2 command (arrays formatted as JSON).close() -> Void
Keys & Strings / JSON / Counters
get(key: String) -> String? | RedisErrorset(key: String, val: String) -> Void | RedisErrorset_ex(key: String, val: String, ttl_secs: Int) -> Void | RedisErrorset_nx(key: String, val: String, ttl_secs?: Int) -> Bool | RedisErrordel(key: String) -> Bool | RedisErrorexists(key: String) -> Bool | RedisErrorexpire(key: String, ttl_secs: Int) -> Bool | RedisErrorttl(key: String) -> Int | RedisErrorincr(key: String) -> Int | RedisErrorincr_by(key: String, delta: Int) -> Int | RedisErrordecr(key: String) -> Int | RedisErrorkeys(pattern: String) -> [String] | RedisErrorget_json<T>(key: String) -> T? | RedisErrorset_json<T>(key: String, val: T, ttl_secs?: Int) -> Void | RedisError
Hashes, Lists & Sets
hset(key: String, field: String, val: String) -> Bool | RedisErrorhget(key: String, field: String) -> String? | RedisErrorhgetall(key: String) -> Map<String, String> | RedisErrorhdel(key: String, field: String) -> Bool | RedisErrorlpush(key: String, val: String) -> Int | RedisErrorrpush(key: String, val: String) -> Int | RedisErrorlpop(key: String) -> String? | RedisErrorrpop(key: String) -> String? | RedisErrorlrange(key: String, start: Int, stop: Int) -> [String] | RedisErrorsadd(key: String, member: String) -> Bool | RedisErrorsrem(key: String, member: String) -> Bool | RedisErrorsmembers(key: String) -> [String] | RedisErrorsismember(key: String, member: String) -> Bool | RedisError
Distributed Locks
lock(key: String, ttl_ms: Int) -> Lock? | RedisError— ExecutesSET key <random_token> NX PX ttl_ms. Returnsnilif lock is already held.unlock(lock: Lock) -> Bool | RedisError— Lua atomic compare-and-delete.extend(lock: Lock, ttl_ms: Int) -> Bool | RedisError— Lua atomic compare-and-PEXPIRE.with_lock(key: String, ttl_ms: Int, cb: () -> R) -> R | RedisError— Acquires lock ("lock_busy"error if unavailable), releases internal connection mutex socb()can run commands on the sameClient, and always runs Lua unlock afterward.