1.3.0 • Published 5 months ago

promise-cachex v1.3.0

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

PromiseCacheX

šŸš€ High-Performance Promise-Based Caching for JavaScript & TypeScript

PromiseCacheX is a lightweight caching library designed to store and manage asynchronous promises and synchronous values efficiently. It eliminates redundant requests, prevents race conditions, and automatically cleans up expired cache entries.


šŸ“š Installation

npm install promise-cachex

šŸ”§ Usage

import { PromiseCacheX } from "promise-cachex";

const cache = new PromiseCacheX({ ttl: 5000, cleanupInterval: 2000 }); // 5s TTL, cleanup every 2s

async function fetchData() {
  return new Promise((resolve) =>
    setTimeout(() => resolve("cached data"), 1000)
  );
}

(async () => {
  const result1 = await cache.get("key1", fetchData, { ttl: 5000 });
  console.log(result1); // 'cached data'

  const result2 = await cache.get("key1", fetchData, { ttl: 5000 });
  console.log(result2); // Returns cached value immediately
})();

// Supports caching synchronous values too
cache.get("key2", "static value");
console.log(await cache.get("key2", "static value")); // 'static value'

⚔ Features

āœ… Promise-Aware – Stores and returns pending promises to avoid duplicate calls. āœ… Supports Both Async and Sync Values – Cache promises, async functions, sync functions, or direct values. āœ… TTL Expiry – Items automatically expire after a configurable time. āœ… Automatic Cleanup – Removes expired entries at a regular interval. āœ… Manual Deletion – Allows explicit cache clearing when needed. āœ… Error Handling – Removes failed promises from the cache. āœ… Efficient & Fast – Optimized for speed and low memory overhead.


🐜 API

constructor(options?: CacheOptions)

Creates a new instance of PromiseCacheX.

OptionTypeDefaultDescription
ttlnumber3600000 (1 hour)Default TTL in milliseconds. 0 means no TTL.
cleanupIntervalnumber300000 (5 minutes)Interval in milliseconds to remove expired items.

get<T>(key: string, fetcherOrPromise: FetchOrPromise<T>, options?: ItemOptions): Promise<T>

Retrieves a cached value or fetches and caches it if not available.

OptionTypeDefaultDescription
ttlnumberCache TTLTTL for the cached item. 0 means no TTL.

FetchOrPromise can be:

  • An async function returning a promise (() => Promise<T>)
  • A synchronous function returning a value (() => T)
  • A direct promise (Promise<T>)
  • A direct value (T)
// Caching an async function
const result = await cache.get("key1", async () => "value", { ttl: 5000 });

// Caching a synchronous function
const syncResult = await cache.get("key2", () => "sync value");

// Caching a direct promise
const promiseResult = await cache.get(
  "key3",
  Promise.resolve("promised value")
);

// Caching a direct value
const directResult = await cache.get("key4", "direct value");

delete(key: string): void

Removes a specific key from the cache.

cache.delete("key1");

clear(): void

Clears all cached entries.

cache.clear();

size(): number

Returns the number of cached items.

console.log(cache.size());

keys(): string[]

Returns an array of cached keys.

console.log(cache.keys());

šŸ“Š Benchmark Results

Here are the latest performance benchmarks for PromiseCacheX:

Operations Performance

TaskLatency Avg (ns)Throughput Avg (ops/s)
Cache 1,000 Keys216,8534,824
Cache 10,000 Keys2,213,626461
Retrieve Cached Values (1,000 keys)221,0394,756
Retrieve Cached Values (10,000 keys)2,136,370476

Memory & CPU Usage

ActionMemory (MB)CPU Time (ms)
After caching 1,000 keys153.2221,296
After caching 10,000 keys208.2239,687
After retrieving 1,000 keys206.8860,765
After retrieving 10,000 keys271.3183,984

šŸ”„ Why Use PromiseCacheX?

  • šŸ† Prevents duplicate async requests (efficient shared promises)
  • ⚔ Fast and lightweight (optimized caching)
  • šŸ›” Ensures memory efficiency (auto-expiring cache)
  • šŸ”„ Great for API calls, database queries, and computations
  • 🌟 Supports both async and sync values (no need for multiple caching libraries)

šŸ“œ License

MIT License.

šŸš€ Try PromiseCacheX today!

1.3.0

5 months ago

1.2.0

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago

0.1.2

5 months ago

0.1.1

6 months ago

0.1.0

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago