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

MethodDescription
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

OptionDefaultDescription
ttlMs1800000 (30 min)Time-to-live in ms.
refreshOnGettrueWhether get refreshes the TTL.
maxKeys1_000_000Capacity cap. Past the cap, oldest entries evict.

See also

Was this page helpful?