redis

The redis module is a Redis client. You connect once to get an integer handle, then pass that handle to every command. Import it with import redis.

Connection

redis.connect returns an int handle on success, or -1 if the URL is invalid or the connection/ping fails. Pass the handle to every other call.

FunctionSignatureDescription
connectredis.connect(url: string) → intconnect via a redis:// or rediss:// URL; returns a handle or -1
closeredis.close(handle: int) → boolclose the connection; true if the handle existed
pingredis.ping(handle: int) → booltrue if the server responds to PING

Key/value

FunctionSignatureDescription
setredis.set(handle: int, key: string, val: any, ttlMs?: int) → boolset a key; ttlMs ≤ 0 or omitted means no expiry
getredis.get(handle: int, key: string) → stringget a value; void if the key is missing
delredis.del(handle: int, key: string) → booldelete a key; true if a key was removed
existsredis.exists(handle: int, key: string) → booltrue if the key exists
expireredis.expire(handle: int, key: string, ttlMs: int) → boolset a TTL in milliseconds
ttlredis.ttl(handle: int, key: string) → intremaining TTL in ms; -1 no expiry, -2 missing
incrredis.incr(handle: int, key: string) → intincrement and return the new value
decrredis.decr(handle: int, key: string) → intdecrement and return the new value
keysredis.keys(handle: int, pattern: string) → string[]keys matching the glob pattern

Lists

FunctionSignatureDescription
lpushredis.lpush(handle: int, key: string, val: any) → intpush to the head; returns new length (-1 on error)
rpushredis.rpush(handle: int, key: string, val: any) → intpush to the tail; returns new length (-1 on error)
lpopredis.lpop(handle: int, key: string) → stringpop from the head; void if empty/missing
rpopredis.rpop(handle: int, key: string) → stringpop from the tail; void if empty/missing
lrangeredis.lrange(handle: int, key: string, start: int, stop: int) → string[]elements in the index range
llenredis.llen(handle: int, key: string) → intlist length

Hashes

FunctionSignatureDescription
hsetredis.hset(handle: int, key: string, field: string, val: any) → boolset a field; true if it was a new field
hgetredis.hget(handle: int, key: string, field: string) → stringget a field; void if missing
hgetallredis.hgetall(handle: int, key: string) → objectall fields as an object (string values)
hdelredis.hdel(handle: int, key: string, field: string) → booldelete a field

Pub/Sub

FunctionSignatureDescription
publishredis.publish(handle: int, channel: string, msg: string) → intpublish a message; returns subscribers reached (-1 on error)

Example

import redis

let db = redis.connect("redis://localhost:6379")
if db < 0 {
    println("could not connect")
    return
}

// Key/value with a 60s TTL
redis.set(db, "session:42", "active", 60000)
println("session: " + redis.get(db, "session:42"))

// A counter
redis.incr(db, "visits")
println("visits: " + toString(redis.incr(db, "visits")))

// A list
redis.rpush(db, "queue", "job-1")
redis.rpush(db, "queue", "job-2")
println("next: " + redis.lpop(db, "queue"))

// Publish to a channel
redis.publish(db, "events", "user-signup")

redis.close(db)
Standard library · View as Markdown · llms-full.txt