Why Caching & Memoization Matter
Caching and memoization are essential for optimizing performance in web applications, reducing redundant computations, and improving response times. However, managing caching across different environmentsโNode.js and browsersโcan be a challenge.
Thatโs why I built crossmemo, a lightweight yet powerful caching and memoization utility designed for seamless cross-environment use. Whether you're optimizing API calls in a Node.js backend or caching UI data in a React app, crossmemo has got you covered.
๐ What is crossmemo?
crossmemo is a flexible and easy-to-use caching solution with built-in TTL (Time-To-Live) support, multiple storage adapters, and LRU (Least Recently Used) eviction strategy. It allows developers to cache function results or store frequently accessed data across different environments.
๐น Key Features
โ
Cross-Environment Support โ Works in Node.js & browsers
โ
Multiple Storage Adapters โ Supports Memory, LocalStorage, and FileSystem
โ
TTL Support โ Set expiration time for cached items
โ
LRU Cache โ Automatically evicts least recently used items
โ
TypeScript Support โ Fully typed API for type safety
โ
Custom Key Resolution โ Flexible cache key generation
โ
Robust Error Handling โ Graceful fallbacks on storage failures
๐ฆ Installation & Quick Start
Getting started with crossmemo is simple:
npm install crossmemo
Memoization Example
import { memoize } from 'crossmemo';
const expensiveFunction = async (x: number) => {
await new Promise(resolve => setTimeout(resolve, 1000));
return x * 2;
};
const memoizedFn = memoize(expensiveFunction, { ttl: 5000 });
await memoizedFn(5); // Takes 1 sec
await memoizedFn(5); // Instant (cached result)
๐ฅ Advanced Usage
Direct Cache Usage
import { Cache } from 'crossmemo';
const cache = new Cache();
await cache.set('key', { data: 'value' }, { ttl: 60000 });
const value = await cache.get('key');
Custom Storage Adapter
import { LocalStorageAdapter } from 'crossmemo';
const cache = new Cache({
adapter: new LocalStorageAdapter({ prefix: 'myapp:', maxSize: 100 })
});
Custom Key Resolution
const memoizedFn = memoize(async (user: { id: number }) => {
return fetchUserData(user.id);
}, {
keyResolver: (user) => `user-${user.id}`
});
๐ฏ Why Use crossmemo?
- Boost Performance โ Reduce redundant function executions
- Save Resources โ Optimize API calls and expensive computations
- Flexible Storage โ Choose the best storage for your environment
- Lightweight & Easy-to-Use โ Simple API with powerful capabilities
๐ Get Started Today
crossmemo is open-source and available on NPM. Try it out and let me know your feedback!
๐ NPM Package: crossmemo on NPM
๐ GitHub Repository: GitHub Repo
Iโd love to hear your thoughts and contributions. Letโs build better caching solutions together! ๐