Skip to content

darshan1005/Cachify

Repository files navigation

memcachify

Lightweight in-memory caching middleware for Node.js APIs (Express + Fastify-ready).

Features

  • 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

Install

npm install memcachify

Quick start (Express)

import 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);

Fastify

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] }));

Client cache helper

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);

Proxy cache helper

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');

API

  • 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.

Build & test

npm install
npm run build
npm run test

About

Make your APIs faster, instantly.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors