JSPM

@nolanx/data-cache

0.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 62
  • Score
    100M100P100Q69627F
  • License MIT

A flexible caching library for JavaScript/TypeScript with multiple storage provider support.

Package Exports

  • @nolanx/data-cache
  • @nolanx/data-cache/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@nolanx/data-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@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

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

๐Ÿ“ฆ 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