Cache

What it is

Nodabase provides Redis-compatible caching through Dragonfly. nodabase.cache is the Node.js interface: store a value under a key, read it back, expire it, check it, delete it.

Values are JSON — store an object and get the same object back. Dragonfly is the underlying service and it speaks the Redis protocol, so the cache can also be inspected with standard Redis tooling.

How it works

Node.js application
       |
       v
  nodabase.cache
       |
       v
   Dragonfly
(Redis-compatible)

set() JSON-encodes the value and writes it to Dragonfly; get() reads it back and parses it. A key that is not there comes back as null.

Configuration

env
NODABASE_CACHE_URL=redis://localhost:6379

NODABASE_CACHE_URL must be set for nodabase.cache to exist on the client. When your application runs inside the Nodabase platform, the binding is provided for you; outside the platform, point it at Dragonfly or any Redis-compatible server.

Basic usage

js
import { nodabase } from "nodabase";
 
await nodabase.cache.set("user:123", { name: "Mahesh" }, { ttl: 30 });
 
const value = await nodabase.cache.get("user:123");
// { name: "Mahesh" }

ttl is a lifetime in seconds and it is optional. Leave it out and the entry stays until you delete it:

js
await nodabase.cache.set("feature:beta", { enabled: true });

Common operations

The whole surface at a glance:

MethodReturnsNotes
set(key, value, { ttl })JSON-encodes value; ttl in seconds
get(key)the parsed value, or null
has(key)true / falsewhether the key exists
delete(key)true / falsetrue when a key was actually removed
expire(key, seconds)true / falseset or replace the expiry of an existing key
ttl(key)seconds remaining-1 when the key has no expiry, -2 when it does not exist
close()closes the connection to Dragonfly

Expiry

js
await nodabase.cache.set("user:123", { name: "Mahesh" }, { ttl: 30 });
 
await nodabase.cache.ttl("user:123"); // seconds left, counting down
 
await nodabase.cache.expire("user:123", 300); // five minutes from now

Existence and deletion

js
await nodabase.cache.has("user:123"); // true or false
 
await nodabase.cache.delete("user:123"); // true if a key was removed

Closing the connection

js
await nodabase.cache.close();

Long-running servers keep the connection open for the life of the process; scripts and one-shot jobs should close it when they are done.

Important notes

Cache or PostgreSQL

PostgreSQL is the source of truth: durable, relational, queried with SQL. The cache is for data you can afford to lose or recompute — a miss should be a performance event, not a data-loss event.

  • Keep in PostgreSQL anything that must survive a restart: user records, orders, everything durable.
  • Put in the cache what is derived: computed lists, rate-limit counters, hot rows that are read far more often than they are written.
  • Never keep the only copy of something in the cache.

Good to know

  • Values must be JSON-serializable — no functions, no streams.
  • get() returns null both for a missing key and for a stored null; use has() when the difference matters.
  • The client exposes these operations and no more — it does not map the full Redis command set. Dragonfly stays reachable directly with any Redis client when you need more.
  • Without NODABASE_CACHE_URL, nodabase.cache is absent from the client — the same pattern as db.

Next steps