Skip to content
Jennifer Programming Language

session API reference

Server-side sessions over a selectable key/value backend. A session is a json.Value held under a sess:ID key with a sliding TTL, so it expires on its own when idle. The store is a kvstore.Store - back it with the in-process kv library (kvstore.inProcessStore / fileStore: single process, no server, both binaries) or a memcache / redis connection (memcache.kvStore / redis.kvStore: distributed, shared across processes, default binary), all behind one API. Session values are a json.Value, so a session holds structured data (nested objects, arrays, numbers), not just flat strings. The JSON is stored base64-wrapped, so any value is binary-safe. Session IDs are UUID v4 from the crypto library's crypto-grade random source (122 unguessable bits), safe to hand a client as the session's bearer token. Sessions are volatile (a cache of soft state, not a store of record - memcache / kv evict, so treat expiry as expected).

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

Contents

NameWhat it is
createMint a new session ID and store an empty session with a ttlSec-second expiry.
destroyRemove the session.
existsWhether the session exists (was created and has not expired or been destroyed).
loadReturn the session's data as a json.Value, or an empty object when the session is absent or expired.
rotateMove a session to a fresh ID and destroy the old one; call it when the privilege level changes (login, logo...
saveWrite the session's data and re-arm its expiry to ttlSec seconds - only while the session exists.
touchRe-arm the session's expiry without rewriting its data.

Functions

create

jennifer
session.create(store as kvstore.Store, ttlSec as int)

Mint a new session ID and store an empty session with a ttlSec-second expiry.

store
the backend store
ttlSec
the session lifetime in seconds (>= 1)
Returns string
the new session ID
Throws Error
kind "session" if ttlSec is < 1

destroy

jennifer
session.destroy(store as kvstore.Store, id as string)

Remove the session.

store
the backend store
id
the session ID
Returns bool
true when the session existed

exists

jennifer
session.exists(store as kvstore.Store, id as string)

Whether the session exists (was created and has not expired or been destroyed).

store
the backend store
id
the session ID
Returns bool
true when the session exists

load

jennifer
session.load(store as kvstore.Store, id as string)

Return the session's data as a json.Value, or an empty object when the session is absent or expired.

store
the backend store
id
the session ID
Returns json.Value
the session data (an empty object if absent or expired)

rotate

jennifer
session.rotate(store as kvstore.Store, id as string, ttlSec as int)

Move a session to a fresh ID and destroy the old one; call it when the privilege level changes (login, logout, sudo) so an ID known before the change is worthless after it. An absent session moves as an empty one.

store
the backend store
id
the current session ID
ttlSec
the new session's lifetime in seconds (>= 1)
Returns string
the new session ID
Throws Error
kind "session" if ttlSec is < 1

save

jennifer
session.save(
    store as kvstore.Store,
    id as string,
    data as json.Value,
    ttlSec as int)

Write the session's data and re-arm its expiry to ttlSec seconds - only while the session exists. A session that was destroyed, rotated away or expired (by a concurrent logout, typically, while this request was in flight) is not written back: that would revive the old id. Nor is an id that was never created, so a client-chosen id is never adopted. Mint sessions with create.

The existence check re-arms the expiry first and then writes, two store calls: a destroy landing between them still loses to the write. kvstore has no write-if-present primitive that would close that window on every backend.

store
the backend store
id
the session ID
data
the session data to store
ttlSec
the session lifetime in seconds (>= 1)
Returns bool
true when written, false when the session no longer exists
Throws Error
kind "session" if ttlSec is < 1

touch

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

Re-arm the session's expiry without rewriting its data.

store
the backend store
id
the session ID
ttlSec
the new lifetime in seconds (>= 1)
Returns bool
true when the session still existed
Throws Error
kind "session" if ttlSec is < 1