fastcache-lite
v1.0.0
Published
A high-performance caching utility for API responses.
Readme
fastcache-lite 🚀
fastcache-lite is a lightweight, zero-dependency TypeScript caching utility designed to optimize website performance. It reduces redundant API calls by caching JSON payloads and static responses in Memory or LocalStorage.
Perfect for:
- Marketing Websites: Cache content-heavy JSON.
- SaaS Dashboards: Speed up user-specific data fetching.
- Internal Admin Panels: Reduce load on low-traffic APIs.
Features
- ✅ Dual-Layer Storage: Automatically switches between Memory and LocalStorage.
- ✅ TTL Support: Set expiration times for cached data.
- ✅ Isomorphic: Works in Node.js and Browser environments.
- ✅ TypeScript Native: Full type safety and autocompletion.
Installation
npm install fastcache-liteQuick Start
1. Basic Usage (Browser/Node)
You can use the getOrFetch method to wrap your API calls.
import { FastCache } from 'fastcache-lite';
const cache = new FastCache({
ttl: 300000, // 5 minutes
useLocalStorage: true
});
async function fetchData(userId: string) {
const url = '/api/profile';
// The cache uses the URL and the params object to manage data
return await cache.getOrFetch(url, { id: userId }, async () => {
const response = await fetch(`${url}/${userId}`);
return response.json();
});
}Development and Testing
Build the package
To compile the TypeScript source into production-ready JavaScript:
npm run buildRunning Tests
To verify the cache logic and key generation:
- Ensure you have
ts-nodeinstalled or use node on the compiled files. - Run the test script:
# Example if using ts-node
npx ts-node tests/collision.test.tsHow it Works: Identity Mapping
fastcache-lite optimizes performance by generating unique identity keys for every request. It maps the combination of the URL and the Request Parameters to a specific cache bucket.
Note: When passing complex objects as parameters, the library utilizes internal stringification to ensure fast lookups.
Folder Structure
| Path | Description |
|------|-------------|
| src/index.ts | The core logic. |
| dist/ | The compiled code (generated after npm run build). |
| tests/ | Your test scripts demonstrating the collision. |
| package.json | Project metadata. |
| tsconfig.json | TypeScript configuration. |
