UNPKG

session-cache-utils

Version:

Lightweight sessionStorage caching utilities for the browser.

90 lines (55 loc) β€’ 2.6 kB
# ⚑️ session-cache-utils > Super lightweight, zero-dependency utility to cache data in `sessionStorage` β€” with automatic TTL expiration, type safety, and full control. --- ## πŸš€ Why use this? - βœ… **Simple**: One function to read, one to write, one to invalidate. That’s it. - 🧠 **Smart**: Auto-expires stale data based on TTL (in minutes). - 🧱 **Type-safe**: Written in TypeScript, strongly typed all the way. - 🌐 **Browser-native**: Uses `sessionStorage`, nothing extra. - πŸ“¦ **Framework-agnostic**: Works in React, Next.js, Svelte, or plain JavaScript apps. --- ## πŸ“¦ Installation ```bash npm install session-cache-utils ``` ## Usage/Examples ```javascript import { writeCache, readCache, invalidateCache } from "session-cache-utils"; //πŸ“ Write to cache writeCache("user-profile", { id: 42, name: "Akshit" }); //πŸ” Read from cache (if not older than 30 minutes) const result = readCache<{ id: number; name: string }>("user-profile", 30); console.log(result?.data); // { id: 42, name: "Akshit" } //❌ Invalidate cache invalidateCache("user-profile"); ``` ## πŸ“– API Reference | Method | Description | Example | | ---------------------- | ----------------------------- | ---------------------------- | | `readCache<T>(key, ttlMinutes)` | Reads cached data and returns it only if it’s still fresh (not older than ttlMinutes) | `readCache<User>("user-data", 30) β†’ { data, created_at, updated_at } or null` | | `writeCache<T>(key, value)` | Writes or updates the cache entry with current timestamps. | `writeCache("user-data", { id: 1, name: "Akshit" }) β†’ true` | | `invalidateCache(key)` | Removes the specified cache entry from sessionStorage | `invalidateCache("user-data") β†’ true` | ## πŸ“¦ CachedDataShape<T> Internal structure used to store cached entries: ```readCache``` api will always return this type for cached entry else ```null``` ```javascript type CachedDataShape<T> = { data: T; created_at: number; updated_at: number; }; ``` ## 🧠 Use Case Examples βš™οΈ Store UI filters temporarily πŸ“„ Cache API response for current tab 🧭 Save navigation state across routes πŸ’Ύ Cache form data between page reloads ## β›” Things to know Only works client-side (window required). TTL is capped at 24 hours internally for sanity. Falls back to null and auto-cleans expired data. ## Authors ✨ Made with focus by Akshit - [@akshitworkspace](https://github.com/akshitworkspace) ## License MIT – free for personal and commercial use.