0.0.5 โข Published 11 months ago
@nolanx/data-cache v0.0.5
@nolanx/data-cache ๐
A lightweight, pluggable caching system for frontend & Node.js applications.
Supports memory, localStorage, sessionStorage, and IndexedDB with TTL-based expiration.
๐ฅ Installation
npm install @nolanx/data-cacheOr
yarn add @nolanx/data-cache๐ Quick Start
import { DataCache, MemoryStore } from "@nolanx/data-cache";
const cache = new DataCache([new MemoryStore()]);
// Store a value for 10 minutes
await cache.set("user", { name: "John" }, 600);
// Get the value
const user = await cache.get("user");
console.log(user); // { name: "John" }๐ Storage Providers
You can choose where data is stored:
- MemoryStore (default) โ Fastest, resets on refresh
- LocalStorageStore โ Persistent across page loads
- SessionStorageStore โ Cleared when the session ends
- IndexedDBStore โ Best for large data storage
Example:
import { DataCache, LocalStorageStore } from "@nolanx/data-cache";
const cache = new DataCache([new LocalStorageStore()]);๐ Supports multiple stores (fallback mechanism):
const cache = new DataCache([
new IndexedDBStore(),
new LocalStorageStore(),
new MemoryStore(),
]);๐ Cache Expiration (TTL)
Set ttl (seconds) to expire cache automatically.
Use ttl = -1 for never-expiring cache.
await cache.set("user", { name: "Alice" }, -1); // Never expiresโก Get or Fetch (getOrSet)
Automatically cache API results:
const user = await cache.getOrSet(
"user",
async () => {
return fetch("/api/user").then((res) => res.json());
},
600
);๐งน Invalidate & Clear Cache
await cache.invalidate("user"); // Remove one key
await cache.clearAll(); // Clear all cache๐ฏ React Hook: useCache
โ Caches API requests in React components
import { useCache } from "@nolanx/data-cache";
import { DataCache, MemoryStore } from "@nolanx/data-cache";
const cache = new DataCache([new MemoryStore()]);
function UserProfile() {
const {
data: user,
loading,
error,
setData: setUser,
} = useCache("user", {
defaultValue: async () => fetch("/api/user").then((res) => res.json()),
ttl: 600, // Optional TTL in seconds, defaults to 60s
cacheInstance: cache,
});
if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error.message}</p>;
return <h1>Welcome, {user.name}!</h1>;
}๐ Supported Methods
| Method | Description |
|---|---|
set(key, value, ttl) | Stores a value with a TTL in seconds |
get(key) | Retrieves a value (removes if expired) |
getOrSet(key, fetcher, ttl) | Fetches & caches if missing |
getAll() | Gets all non-expired cache entries |
invalidate(key) | Removes a specific key from cache |
clearAll() | Clears all cached data |
clearExpired() | Clears expired cache entries |
๐ฆ Publish & Usage
Once installed, you can import the library like this:
import { DataCache, MemoryStore, useCache } from "@nolanx/data-cache";๐ Supports both ES Modules & CommonJS.
๐ฅ Why @nolanx/data-cache?
โ
Pluggable storage (Memory, LocalStorage, IndexedDB)
โ
TTL-based expiration for automatic cleanup
โ
Lightweight (~3KB) & fast performance
โ
Fully typed with TypeScript
โ
React Hook (useCache) support
๐ License
MIT ยฉ 2025 @nolanx