npm.io
1.0.2 • Published 8 months ago

nx-cache

Licence
ISC
Version
1.0.2
Deps
0
Size
8 kB
Vulns
0
Weekly
0

nx-cache

Minimal in-memory cache SDK for Node.js.

No setup. No config. Just use it.


Install

npm i nx-cache

Usage

import { createNxCache } from "nx-cache";

const cache = createNxCache();

Write

cache.write(
  "policy",
  { allow: true },
  {
    scope: { orgId: "acme", env: "prod" },
    metadata: { source: "api" },
  }
);
  • Same key + different scope = different entry
  • Scope can be null

Read

const res = cache.read("policy", { orgId: "acme", env: "prod" });

if (res.found) {
  console.log(res.value);
  console.log(res.metadata);
  console.log(res.meta.savedAt);
}

Without scope:

cache.read("policy"); // equivalent to scope = null

const results = cache.search("policy", { orgId: "acme", env: "prod" });

for (const r of results) {
  console.log(r.value, r.metadata);
}

Reset

cache.resetKey("policy", { orgId: "acme", env: "prod" });
cache.resetAll();

Behavior summary

  • Default TTL: 1 year
  • Expired entries are treated as not found
  • Metadata is user-defined and returned on read/search
  • Cache is per-process (memory only)

Future extensions (not in v1)

  • Redis backend
  • Disk persistence
  • Stale reads
  • Namespaced caches

Keywords