0.0.5 โ€ข Published 11 months ago

@nolanx/data-cache v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@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-cache

Or

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

MethodDescription
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

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago