firestore-read-cache

Implements TTL-bounded in-memory caching for repeated Firestore reads in long-lived Node processes.

Updated May 13, 2026
One-click install
npx skills add https://github.com/Hinten/next_erp --skill firestore-read-cache-hinten
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: firestore-read-cache
Source: https://github.com/Hinten/next_erp/tree/main/.claude/skills/firestore-read-cache
Command: npx skills add https://github.com/Hinten/next_erp --skill firestore-read-cache-hinten

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Repeated Firestore reads in long-lived server processes (Cloud Functions, App Hosting) inflate billed data scanned and latency, especially for config documents and queries that rarely change. This Skill guides safe adoption of the @delfrance/data/admin/cache read-cache primitive while avoiding the reads that must never be cached. ## Core Features & Use Cases - Three cache shapes: createCachedDocReader for config documents, createReadCache with tuple keys for queries, and arbitrary loaders for aggregates or pipelines. - Hard exclusion rules: never cache transactional tx.get reads, read-modify-write flows, or OAuth token lookups, with live repo examples for each. - TTL and freshness policy: READ_CACHE_TTL tiers, negative caching controls, isFresh re-validation, per-instance invalidation on self-writes, and a DATA_READ_CACHE_DISABLED kill switch. - Use Case: A channel context loader like loadMercadoLivreContext re-reads the same integracao document on every webhook; wrap the read in a module-scope cached reader with a 15-minute TTL and invalidate it after the OAuth callback merges user_id. ## Quick Start Ask the assistant to apply the firestore-read-cache guidance to add a cached reader for a repeated config document read in a channel context loader.

Frequently Asked Questions about firestore-read-cache

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I cache Firestore reads in Node.js Cloud Functions?

Create a module-scope cache with createReadCache or createCachedDocReader from @delfrance/data/admin/cache, keyed by document path or query predicate values. Module scope matters because Cloud Functions gen2 instances persist across invocations, so the cache survives between requests.

Which Firestore reads should never be cached?

Never cache reads inside runTransaction, read-modify-write flows, or OAuth token lookups. A cached tx.get drops the document from the transaction read set, a cached read-modify-write decides on stale data, and a cached token can resurrect a rotated single-use refresh token.

How do I handle cache invalidation after writing a cached document?

Call the reader's invalidate method with the same key arguments immediately after the write, as exchangeAndPersist does after merging user_id onto the integracao document. This only clears the local instance; other warm instances rely on the TTL or an isFresh predicate.

Does the Firestore read cache work across multiple server instances?

No, the cache is process-scoped and instances do not coordinate. The TTL is the staleness bound for other instances, and isFresh re-validation on each hit handles fields with tighter freshness requirements without cross-instance messaging.

How do I test TTL expiration without sleeping in tests?

Inject a controllable clock via the now option when creating the cache, then advance the fake time past ttlMs and assert a second read occurs. Also call __resetAllReadCaches in beforeEach and afterEach so cached state never leaks between tests in one process.

When should negative caching be disabled for Firestore queries?

Set negativeTtlMs to 0 whenever an absent result drives an irreversible decision, such as resolveIntegracaoByUserId returning no-account which deterministically acks and drops notifications. Caching that miss would drop events for a seller who connected seconds earlier.