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)