Session
createSession() is an in-process key-value session store with sliding TTL. Reads refresh the TTL by default; writes always do.
For cluster-wide sessions, use createDistributedSession from the extensions package - the API shape is the same so callers can swap implementations.
Setup
// src/lib/server/session.js
import { createSession } from 'svelte-adapter-uws/plugins/session';
export const sessions = createSession({ ttlMs: 30 * 60_000 }); Usage
import { sessions } from '$lib/server/session';
await sessions.set('user:42', { name: 'Alice', role: 'admin' });
const data = await sessions.get('user:42'); // { name: 'Alice', role: 'admin' }
// TTL refreshed by the get
await sessions.delete('user:42'); API
| Method | Description |
|---|---|
get(key) | Returns the stored value or undefined. Refreshes TTL when refreshOnGet is true (default). |
set(key, value, { ttlMs? }) | Store a value. Optional per-call TTL override. |
delete(key) | Remove a key. |
touch(key) | Refresh the TTL without reading the value. |
clear() | Drop all entries. |
Options
| Option | Default | Description |
|---|---|---|
ttlMs | 1800000 (30 min) | Time-to-live in ms. |
refreshOnGet | true | Whether get refreshes the TTL. |
maxKeys | 1_000_000 | Capacity cap. Past the cap, oldest entries evict. |
See also
createDistributedSession- Redis-backed cluster-wide variant.
Was this page helpful?