Skip to content

Storage caches and sessions ​

Choose a namespace before using storage. Call configureStorage before the first Local or Session operation; omit it when the default fast__ prefix is suitable.

Configure the namespace at application startup ​

ts
import { configureStorage } from "@fast-china/utils";

configureStorage({ prefix: "my-app:" });

Configuration cannot change after activation. Do not reconfigure it on every page or request.

Store an expiring cache entry ​

ts
import { Local } from "@fast-china/utils";

interface Profile {
	id: number;
	name: string;
}

Local.set("profile", { id: 1, name: "Fast" }, { ttlMs: 30 * 60 * 1000 });
const profile = Local.get<Profile>("profile");

if (profile !== undefined) {
	console.log(profile.name);
}

The generic does not validate data at runtime. Validate values from untrusted storage. Missing or expired keys are cache misses; quota and decoding errors must not be disguised as success.

Observe a hit, expiry and read failure ​

Run this on a separate browser test page. The example advances a controlled clock and uses a fresh namespace.

ts
import { configureStorage, Local } from "@fast-china/utils";

let now = 0;
const prefix = "fast-docs-cache-" + crypto.randomUUID() + ":";
configureStorage({ prefix, now: () => now });

Local.set("profile", { name: "Fast" }, { ttlMs: 1000 });
console.log(Local.get("profile")); // { name: "Fast" }

now += 1001;
console.log(Local.get("profile")); // undefined

const brokenKey = prefix + "broken";
localStorage.setItem(brokenKey, "invalid-json");
try {
	Local.get("broken");
} catch (error) {
	console.error("Read failed", error);
} finally {
	localStorage.removeItem(brokenKey);
}

A hit returns the stored value, expiry returns undefined, and corrupt data throws. Handle the exception at the application boundary or call site when you need to distinguish it from a cache miss.

Session values and cleanup ​

Browser Session storage can hold session-only drafts; uni-app has no corresponding backend. Use Local.remove("profile") for a single entry. Call Local.clear() only when the entire application namespace should be cleared; do not clear shared underlying storage directly.

Codec and security boundary ​

Per-operation crypto options must match on writes and reads. This option uses reversible Base64 obfuscation, not encryption, and must not protect secrets.

See Storage API for exact signatures, defaults, errors and platform differences. This guide maintains the workflow, not a duplicate parameter reference.