Small state, scoped to a user

cloud.kv

Values are stored as JSON and come back with their types intact — a number is a number, an object is an object.

Keys are namespaced server-side as {appId}:kv:{userId}:{key}. The user id comes from the session cookie and is never read from the request, so one user cannot name another user’s key.

Editor
Output
Press Run.
Network — real requests to this site’s own backend
No requests yet.

get(key) → T | null

Returns null for a missing key. Absence is not an error.

set(key, value, { ttl })

Stores any JSON value. ttl is in seconds.

del(key)

Removes a key.

incr(key, by = 1) → number

Atomic server-side increment. Safe under concurrency, unlike get-then-set.

decr(key, by = 1) → number

The same, downwards.

list({ prefix, cursor, limit })

One page of keys with the namespace stripped. Backed by SCAN, never KEYS.

keys(prefix = "") → string[]

Every matching key, following the cursor for you. Convenient, but O(number of keys) round trips — use list once a namespace gets large.

flush()

Deletes every key belonging to the signed-in user, and nobody else’s.