flamecache
v2.0.0
Published
Redis-style caching layer over the Firebase Realtime Database REST API. Zero dependencies. Runs in Cloudflare Workers, Deno, Bun, Node and browsers.
Maintainers
Readme
Flamecache
A Redis-style cache for Firebase Realtime Database, built on plain fetch.
No dependencies. Runs in Cloudflare Workers, Deno, Bun, Node 20+ and browsers.
Install
npm install flamecacheQuick start
import { createCache, serviceAccountAuth } from 'flamecache';
const cache = createCache({
databaseUrl: 'https://my-app-default-rtdb.firebaseio.com',
auth: serviceAccountAuth(process.env.FIREBASE_SERVICE_ACCOUNT!), // key file JSON
});
await cache.set('user:123', { name: 'Ada' }, 600); // ttl in seconds
await cache.get('user:123'); // { name: 'Ada' }
await cache.incr('views:home'); // atomic, returns the new value
await cache.wrap('posts', () => loadPosts(), 300); // cached read-throughIn the browser
Pass the signed-in user's Firebase ID token:
const cache = createCache({
databaseUrl: 'https://my-app-default-rtdb.firebaseio.com',
auth: ({ forceRefresh }) => user.getIdToken(forceRefresh),
rootPath: 'flamecache/shared',
readOnly: true,
});Deploy database.rules.json first. It lets browsers
read a cache your server writes, and optionally keep their own validated
entries. See Security rules.
API
| Method | What it does |
| ----------------------------------------------------- | ------------------------------------------------- |
| get(key), mget(keys) | Read values; null when missing or expired |
| set(key, value, ttl?), mset(entries, ttl?) | Write values; mset is atomic |
| del(key), mdel(keys), clear() | Delete; mdel is atomic |
| has(key) | true when present and not expired |
| incr(key, by?, ttl?), decr(key, by?, ttl?) | Atomic counters; return the new value |
| expire(key, ttl), touch(key, ttl?), getTtl(key) | Change or read the expiry |
| wrap(key, fn, ttl?) | Return the cached value, or run fn and cache it |
| pull(key) | Read and delete in one atomic step |
| keys() | List the keys under the root |
| sweep() | Delete expired entries; run it from a cron |
| scope(path) | A cache rooted at a sub-path |
Failures throw a FlamecacheError with a code such as permission-denied.
wrap is the exception: if the cache fails it still returns fn()'s result.
Options
| Option | Default | Description |
| --------------- | -------------- | -------------------------------------------- |
| databaseUrl | required | Your Realtime Database URL |
| auth | none | Token, or ({ forceRefresh }) => token |
| rootPath | 'flamecache' | Where entries are stored |
| ttl | 3600 | Default expiry in seconds; 0 never expires |
| readOnly | false | Reject writes, for browser clients |
| valueEncoding | 'json' | Use 'string' in rule-validated tiers |
| disableCache | false | Make every call a miss |
| debug | false | Log requests, with tokens redacted |
Timeouts, retries, App Check and a custom fetch are covered in the
guide.
Docs
- Guide: authentication, expiry and sweeping, atomicity, errors
- Security rules: browser tiers and what each rule prevents
- Performance: measured latency and bundle size against the Firebase SDK
- Migrating from v1
Development
npm install
npm test # offline, no Firebase project needed
npm run lint && npm run typecheck && npm run buildnpm run test:integration and npm run bench use a real database. Copy
.env.example to .env to configure them.
License
MIT © Anish Roy
