Lightweight in-memory caching middleware for Node.js APIs (Express + Fastify-ready).
- In-memory Map store with O(1) lookups
- TTL-based expiration
- Cache invalidation by key or predicate
- Auto cleanup of expired entries
- Express middleware with painless integration
- API for custom store adapters in future
npm install memcachifyimport express from 'express';
import { cache, invalidate, clearCache } from 'memcachify';
const app = express();
app.get('/users', cache({ ttl: 60 }), (req, res) => {
res.json({ data: [1, 2, 3] });
});
app.post('/users', (req, res) => {
// write logic...
invalidate((key) => key.includes('/users'));
res.status(201).send('created');
});
app.listen(3000);import Fastify from 'fastify';
import { fastifyCache } from 'memcachify';
const app = Fastify();
const store = new InMemoryStore({ defaultTTL: 60 });
await app.register(fastifyCache({ store }));
app.get('/users', async () => ({ data: [1,2,3] }));import { ClientCache } from 'memcachify';
const cache = new ClientCache<{ data: string }>({ ttlMs: 60_000 });
cache.set('userCache', { data: 'hello' });
const value = cache.get('userCache');
console.log(value);import { InMemoryStore, fetchWithCache } from 'memcachify';
const store = new InMemoryStore({ defaultTTL: 60 });
const response = await fetchWithCache('https://httpbin.org/get', {}, { store, ttl: 60 });
if (response.cached) console.log('from cache');cache(options?)— Express middleware (default TTL 60s).invalidate(keyOrPredicate)— invalidate a key or predicate in shared store.clearCache()— flush all entries.InMemoryStore— class for custom stores or multiple stores.
npm install
npm run build
npm run test