Nyx by Example

nyx-kv TTL and EXPIRE

EXPIRE sets a TTL on a key — after the given seconds elapse, the key is auto-deleted. Essential for session storage, cache, and rate limiting.

Code

// nyx-kv TTL -- key expiration for caching and session storage

fn resp_cmd(parts: Array) -> String {
    var sb: StringBuilder = StringBuilder.new()
    sb.append("*")
    sb.append(int_to_string(parts.length()))
    sb.append("\r\n")
    var i: int = 0
    while i < parts.length() {
        let p: String = parts[i]
        sb.append("$")
        sb.append(int_to_string(p.length()))
        sb.append("\r\n")
        sb.append(p)
        sb.append("\r\n")
        i = i + 1
    }
    return sb.to_string()
}

fn main() -> int {
    let fd: int = tcp_connect("127.0.0.1", 6380)
    if fd < 0 {
        print("connection failed")
        return 1
    }

    // Store a session token that expires in 60 seconds
    tcp_write(fd, resp_cmd(["SET", "session:abc123", "user42"]))
    let set_reply: String = tcp_read_line(fd)
    print("SET -> " + set_reply.trim())

    // Set expiration (in seconds)
    tcp_write(fd, resp_cmd(["EXPIRE", "session:abc123", "60"]))
    let expire_reply: String = tcp_read_line(fd)
    print("EXPIRE 60s -> " + expire_reply.trim())

    // Check remaining TTL
    tcp_write(fd, resp_cmd(["TTL", "session:abc123"]))
    let ttl: String = tcp_read_line(fd)
    print("TTL -> " + ttl.trim() + " seconds")

    // Alternative: SET with EX in one command
    tcp_write(fd, resp_cmd(["SET", "cache:user:42", "payload", "EX", "300"]))
    let setex: String = tcp_read_line(fd)
    print("SET with EX 300 -> " + setex.trim())

    // Remove expiration (make key permanent)
    tcp_write(fd, resp_cmd(["PERSIST", "session:abc123"]))
    let persist: String = tcp_read_line(fd)
    print("PERSIST -> " + persist.trim())

    tcp_close(fd)
    return 0
}

Output

SET -> +OK
EXPIRE 60s -> :1
TTL -> :60 seconds
SET with EX 300 -> +OK
PERSIST -> :1

Explanation

Every key can carry a TTL — a monotonic clock deadline after which the server lazily deletes it. EXPIRE key seconds attaches a TTL to an existing key and returns :1 if the key existed or :0 otherwise. TTL key returns the remaining seconds, -2 if the key does not exist, and -1 if it is permanent.

The combined form SET key value EX seconds sets the value and TTL atomically, which is what you want for caches — you never leave a window where the key exists without expiry. To cancel a pending expiration use PERSIST key.

TTLs are the foundation for session storage (auto-logout), rate limiting (INCR + EXPIRE on first hit), idempotency keys, and cache lines that need to forget stale data without a sweeper.

← Previous Next →

Source: examples/by-example/76-kv-expire.nx