Skip to content
Jennifer Programming Language

kvstore API reference

A key/value store with per-key TTL behind one Store value and one verb set (set / get / delete / touch / incrWindow), so code written against it runs over any backend. This is the shared backend layer under the session and ratelimit modules. The verb set mirrors memcache, so a fixed-window counter (atomic incr + TTL) works identically on every backend.

This module holds the local backends, over the kv library, and runs on both binaries: inProcessStore keeps state in memory (reset each run), fileStore persists it to a file (survives across jennifer run invocations; handy for development and CLI tools with no server). The distributed backends, a memcache or redis connection shared across processes, are built by the client modules, memcache.kvStore(mc) and redis.kvStore(rc), which need the default binary (net); they return the same Store, so session and ratelimit take either.

Store is a sum type (enum): a Local variant carries its kv.Store handle, a Remote variant the Backend that memcache or redis fills in (the connection and one function value per verb, which run in that module). This module imports no network module: use net; names only the net.Conn type, so it loads on jennifer-tiny too.

Import with import "kvstore.j" as kvstore;. See the kvstore guide for prose and examples.

Contents

NameWhat it is
deleteRemove key; returns whether it existed.
fileStoreA store backed by the in-process kv library, persisted to path: its contents load on open and survi...
getReturn the value at key, or "" when absent / expired.
inProcessStoreA store backed by the in-process kv library (this process only, no server).
incrWindowAtomically increment the counter at key and return the new value, creating it (at 1, with a ttlSec-seco...
remoteStoreA store over a remote backend.
setStore value at key with a ttlSec-second expiry (0 = no expiry), replacing any existing value.
touchRe-arm key's expiry to ttlSec seconds; returns whether it existed.
Backendstruct: A remote backend, as memcache.kvStore and redis.kvStore build it: the connection and the function value...
Storeenum: A store bound to one backend.

Functions

delete

jennifer
kvstore.delete(store as Store, key as string)

Remove key; returns whether it existed.

store
the backend store
key
the key to remove
Returns bool
whether the key existed

fileStore

jennifer
kvstore.fileStore(path as string)

A store backed by the in-process kv library, persisted to path: its contents load on open and survive across jennifer run invocations (unlike inProcessStore, which resets each run). No server; single-process (safe for spawn within one process, but not for concurrent separate processes on one file - use a distributed backend for that). Practical for development and persistent CLI state.

path
the file to persist the store to
Returns Store
a file-backed local store

get

jennifer
kvstore.get(store as Store, key as string)

Return the value at key, or "" when absent / expired.

store
the backend store
key
the key to read
Returns string
the value, or "" when absent / expired

inProcessStore

jennifer
kvstore.inProcessStore()

A store backed by the in-process kv library (this process only, no server). Opens a fresh kv.Store; keep the returned Store for the program's lifetime.

Returns Store
an in-process store

incrWindow

jennifer
kvstore.incrWindow(store as Store, key as string, ttlSec as int)

Atomically increment the counter at key and return the new value, creating it (at 1, with a ttlSec-second expiry) on the first hit. The atomic-counter-plus-TTL primitive a fixed-window rate limiter is built from - portable across every backend (kv add then incr; see memcache.kvStore and redis.kvStore).

store
the store
key
the counter key
ttlSec
the TTL to arm on the first hit (seconds)
Returns int
the counter's new value
Throws Error
kind "kvstore" for a negative ttlSec

remoteStore

jennifer
kvstore.remoteStore(backend as Backend)

A store over a remote backend. memcache.kvStore and redis.kvStore call this; other code has no need to.

backend
the backend, with every verb set
Returns Store
a store dispatching to the backend

set

jennifer
kvstore.set(store as Store, key as string, value as string, ttlSec as int)

Store value at key with a ttlSec-second expiry (0 = no expiry), replacing any existing value.

store
the backend store
key
the key to write
value
the value to store
ttlSec
the expiry in seconds (0 = no expiry)
Throws Error
kind "kvstore" for a negative ttlSec

touch

jennifer
kvstore.touch(store as Store, key as string, ttlSec as int)

Re-arm key's expiry to ttlSec seconds; returns whether it existed.

store
the backend store
key
the key to re-arm
ttlSec
the new expiry in seconds (0 = no expiry)
Returns bool
whether the key existed
Throws Error
kind "kvstore" for a negative ttlSec

Structs

Backend

A remote backend, as memcache.kvStore and redis.kvStore build it: the connection and the function value that carries out each verb on it. Not built by hand; the function values are called with the Backend itself first, then the verb's arguments.

jennifer
def struct Backend {
    name as string,
    conn as net.Conn,
    timeoutMs as int,
    set as func,
    get as func,
    delete as func,
    touch as func,
    incrWindow as func
};
name
the backend: "memcache" or "redis"
conn
the open connection
timeoutMs
the connection's per-reply timeout in milliseconds
set
(Backend, key, value, ttlSec)
get
(Backend, key) -> string
delete
(Backend, key) -> bool
touch
(Backend, key, ttlSec) -> bool
incrWindow
(Backend, key, ttlSec) -> int

Enums

Store

A store bound to one backend. Value-semantic; the handle a variant wraps (a kv.Store or a net.Conn) is shared across copies. Build with inProcessStore / fileStore, or memcache.kvStore / redis.kvStore.