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 memcache or redis (distributed, shared across processes) or the in-process kv library (single process, no server), 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.
Functions
session.create(store as kvstore.Store, ttl as int)
Mint a new session ID and store an empty session with a ttl-second expiry.
Parameters
store{kvstore.Store}- the backend storettl{int}- the session lifetime in seconds
Returns {string} - the new session ID
session.destroy(store as kvstore.Store, id as string)
Remove the session.
Parameters
store{kvstore.Store}- the backend storeid{string}- the session ID
Returns {bool} - true when the session existed
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.
Parameters
store{kvstore.Store}- the backend storeid{string}- the session ID
Returns {json.Value} - the session data (an empty object if absent or expired)
session.save(store as kvstore.Store, id as string, data as json.Value, ttl as int)
Write the session's data and re-arm its expiry to ttl seconds.
Parameters
store{kvstore.Store}- the backend storeid{string}- the session IDdata{json.Value}- the session data to storettl{int}- the session lifetime in seconds
session.touch(store as kvstore.Store, id as string, ttl as int)
Re-arm the session's expiry without rewriting its data.
Parameters
store{kvstore.Store}- the backend storeid{string}- the session IDttl{int}- the new lifetime in seconds
Returns {bool} - true when the session still existed