1.0.0 • Published 3 months ago
cache-manager-js v1.0.0
cache-manager-js
A caching library for managing asynchronous function results with TTL and eviction strategies.
Installation
You can install the package using npm:
npm install cache-manager-js
Usage
import { AsyncCacheManager } from "scache-manager-js";
const cache = new AsyncCacheManager();
const callCount: any[] = [];
async function fetchDataFromAPI(id: string): Promise<any> {
console.log(
`async function called. ID: ${id} Count: ${callCount.length + 1}`
);
const result = Math.random();
callCount.push(result);
return result;
}
const cachedFetchData = cache.wrap(fetchDataFromAPI);
// The first call triggers the actual async operation
const data1 = await cachedFetchData("123");
// Subsequent calls return cached results without re-executing the async operation
const data2 = await cachedFetchData("123");
const data3 = await cachedFetchData("666");
if (data1 === data2) {
console.log("data1 equals data2");
// data1 equals data2
}
Promising Field
Asynchronous programming is increasingly prevalent in modern JavaScript and TypeScript applications. Creating a sophisticated caching manager for asynchronous operations addresses a common performance optimization challenge. This package could be particularly useful in scenarios where repeated asynchronous calls with similar parameters occur.
License
This project is licensed under the MIT License - see the LICENSE file for details.