⚡ [Cache getLatestDate to reduce redundant Firestore queries]#101
⚡ [Cache getLatestDate to reduce redundant Firestore queries]#101max-ostapenko wants to merge 3 commits intomainfrom
Conversation
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
Adds an in-memory TTL cache to getLatestDate to avoid repeated Firestore reads for semi-static “latest date” lookups, improving latency and reducing database load on hot paths.
Changes:
- Introduces a module-level
Mapcache keyed by collection name. - Applies a 1-hour TTL to cached
getLatestDateresults. - Writes successful query results into the cache before returning.
Comments suppressed due to low confidence (1)
src/utils/controllerHelpers.js:75
- Caching behavior (TTL expiry and cache hits) is new logic in
getLatestDatebut there are currently no unit tests covering it. Add Jest tests that mock the Firestore query and verify: (1) second call within TTL doesn’t callquery.get()again, and (2) after TTL elapses it re-queries.
const getLatestDate = async (firestore, collection) => {
const now = Date.now();
const cached = latestDateCache.get(collection);
// Return cached date if it exists and hasn't expired
if (cached && (now - cached.timestamp < LATEST_DATE_CACHE_TTL)) {
return cached.date;
}
// Query for latest date
const query = firestore.collection(collection).orderBy('date', 'desc').limit(1);
const snapshot = await query.get();
if (!snapshot.empty) {
const date = snapshot.docs[0].data().date;
// Update cache
latestDateCache.set(collection, { date, timestamp: now });
return date;
}
return null;
};
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const getLatestDate = async (firestore, collection) => { | ||
| const now = Date.now(); | ||
| const cached = latestDateCache.get(collection); | ||
|
|
||
| // Return cached date if it exists and hasn't expired | ||
| if (cached && (now - cached.timestamp < LATEST_DATE_CACHE_TTL)) { | ||
| return cached.date; | ||
| } | ||
|
|
||
| // Query for latest date | ||
| const query = firestore.collection(collection).orderBy('date', 'desc').limit(1); | ||
| const snapshot = await query.get(); | ||
|
|
There was a problem hiding this comment.
This cache only memoizes completed results; if multiple requests hit getLatestDate concurrently when the cache is cold/expired, each call will still execute its own Firestore query. Consider caching an in-flight Promise per collection (and clearing it on resolve/reject) so concurrent callers share the same query result.
| // Update cache | ||
| latestDateCache.set(collection, { date, timestamp: now }); |
There was a problem hiding this comment.
now is captured before the Firestore read; if the query takes noticeable time, the cached entry’s TTL is effectively shortened by the query duration. Capture the timestamp after the snapshot is successfully retrieved (or store an expiresAt value) so the TTL reflects the actual cache insertion time.
| // Update cache | |
| latestDateCache.set(collection, { date, timestamp: now }); | |
| const cacheTimestamp = Date.now(); | |
| // Update cache | |
| latestDateCache.set(collection, { date, timestamp: cacheTimestamp }); |
💡 What:
Implemented an in-memory
Mapcache with a 1-hour Time-To-Live (TTL) for thegetLatestDatefunction insrc/utils/controllerHelpers.js.🎯 Why:
The
getLatestDatefunction queries thedateof the latest document in a collection. Since this data is semi-static (often updated monthly in HTTP Archive) but the route is accessed frequently, doing a Firestore query on every execution introduces unnecessary network delay and database read operations. By caching this value, we significantly reduce latency and database load.📊 Measured Improvement:
Created a benchmark script simulating 50ms of network/DB latency per query and executed 100 iterations:
PR created automatically by Jules for task 11540001056034819506 started by @max-ostapenko