Image description

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! ๐Ÿš€

NodeJS #ReactJS #TypeScript #Caching #Memoization #WebPerformance #SoftwareDevelopment