DEV Community

Cover image for My new NPM package for cache 🚀
subhadip pahari
subhadip pahari

Posted on

My new NPM package for cache 🚀

Introducing flexi-cache-node: A Modern, Zero-Dependency Replacement for node-cache.

Caching is one of those invisible building blocks that keeps applications fast, reliable, and efficient. If you’ve ever used node-cache, you know how handy it can be — but also how limited.

That’s why I built flexi-cache-node : a zero-dependency, TypeScript-ready caching library for Node.js with modern features baked in.


Why I built it 💡

I needed a cache that:

  1. could expire keys (TTL) reliably,

  2. had LRU eviction so memory wouldn’t blow up,

  3. supported tags so I could bulk-delete groups of keys,

  4. could persist data to disk securely,

  5. worked seamlessly in both JavaScript and TypeScript,

  6. and most importantly… had zero dependencies.

Instead of bolting these features on top of node-cache, I decided to start fresh.


What makes flexi-cache-node different ✨

-- ⏱ TTL (Time-To-Live) per key or global

-- ♻ LRU eviction to keep size under control

-- 🏷 Tag-based grouping for smarter key management

-- 💾 Disk persistence with crash-safe writes

-- 🔒 AES-256-GCM encryption for at-rest data security

-- 📊 Stats & events — track hits, misses, expirations

-- 🔧 Works with JavaScript & TypeScript out of the box

-- 🪶 Zero dependencies — smaller, safer, faster

Think of it as the caching library I wish existed when I started out.


Installation

npm install flexi-cache-node
# or
yarn add flexi-cache-node
Enter fullscreen mode Exit fullscreen mode

Please check out the examples for practical use cases in various scenarios.


example 1: Basic in-memory cache

const NodeCache = require("flexi-cache-node");

const cache = new NodeCache({ stdTTL: 5 });
cache.set("foo", "bar");

console.log(cache.get("foo")); // "bar"

Enter fullscreen mode Exit fullscreen mode

example 2: Tag-based cache

const { TagCache } = require("flexi-cache-node");

const tc = new TagCache();
tc.setWithTags("user:1", { name: "Alice" }, ["active", "premium"]);
tc.setWithTags("user:2", { name: "Bob" }, ["active"]);

console.log(tc.getValuesByTag("active"));
// → [ { name: "Alice" }, { name: "Bob" } ]

Enter fullscreen mode Exit fullscreen mode

example 3: LRU eviction

const { LRUCache } = require("flexi-cache-node");

const lru = new LRUCache({ size: 2 });
lru.set("a", 1);
lru.set("b", 2);
lru.get("a");
lru.set("c", 3); // evicts "b"

console.log(lru.getKeys()); // [ "a", "c" ]

Enter fullscreen mode Exit fullscreen mode

example 4: Persistence with encryption

const NodeCache = require("flexi-cache-node");

const cache = new NodeCache({
  encryption: true,
  secretKey: "superSecretKey123",
  persistPathFolder: {
    type: "disk",
    diskConfig: { folderLocation: "./cache-data" }
  }
});

cache.set("token", { id: 123 });
await cache.flush(); // writes encrypted snapshot

Enter fullscreen mode Exit fullscreen mode

Coming soon 🔮

Browser-based cache (using LocalStorage & SessionStorage)

Cloud persistence (S3, GCP, Azure)

Compression for large values


Final thoughts ❤️

Please give flexi-cache-node a try and let me know any feedback.
I’m open to modifying or improving it based on your suggestions — let’s make caching in Node.js a little less boring and a lot more powerful together ✨.

Top comments (0)