Skip to content

Commit 14328bf

Browse files
committed
query cache
1 parent c0e696d commit 14328bf

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

src/controllers/cwvtechController.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { firestoreOld } from '../utils/db.js';
22
const firestore = firestoreOld;
33

44
import {
5-
getLatestDate
5+
getLatestDate,
6+
generateQueryCacheKey,
7+
getCachedQueryResult,
8+
setCachedQueryResult
69
} from '../utils/controllerHelpers.js';
710

811
const TABLE = 'core_web_vitals';
@@ -37,6 +40,24 @@ const listCWVTechData = async (req, res) => {
3740
startDate = await getLatestDate(firestore, TABLE);
3841
}
3942

43+
// Create cache key for this specific query
44+
const queryFilters = {
45+
geo: params.geo,
46+
rank: params.rank,
47+
technology: techArray,
48+
startDate: startDate,
49+
endDate: params.end
50+
};
51+
const cacheKey = generateQueryCacheKey(TABLE, queryFilters);
52+
53+
// Check cache first
54+
const cachedResult = getCachedQueryResult(cacheKey);
55+
if (cachedResult) {
56+
res.statusCode = 200;
57+
res.end(JSON.stringify(cachedResult));
58+
return;
59+
}
60+
4061
// Build optimized query
4162
let query = firestore.collection(TABLE);
4263

@@ -49,7 +70,7 @@ const listCWVTechData = async (req, res) => {
4970
// Use 'in' operator for batch processing (Firestore limit: 30 values)
5071
query = query.where('technology', 'in', techArray);
5172
} else {
52-
// Parallel queries for >10 technologies (rare case)
73+
// Parallel queries for >30 technologies (rare case)
5374
const queryPromises = techArray.map(async (technology) => {
5475
let individualQuery = firestore.collection(TABLE)
5576
.where('geo', '==', params.geo)
@@ -68,6 +89,9 @@ const listCWVTechData = async (req, res) => {
6889
const results = await Promise.all(queryPromises);
6990
const data = results.flat();
7091

92+
// Cache the result
93+
setCachedQueryResult(cacheKey, data);
94+
7195
res.statusCode = 200;
7296
res.end(JSON.stringify(data));
7397
return;
@@ -84,6 +108,9 @@ const listCWVTechData = async (req, res) => {
84108
data.push(doc.data());
85109
});
86110

111+
// Cache the result
112+
setCachedQueryResult(cacheKey, data);
113+
87114
// Direct response without wrapper functions
88115
res.statusCode = 200;
89116
res.end(JSON.stringify(data));

src/utils/controllerHelpers.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,45 @@ const sendValidationError = (res, errors) => {
4242
const latestDateCache = new Map();
4343
const CACHE_TTL = 60 * 60 * 1000; // 1 hour in milliseconds
4444

45+
// Cache for query results to eliminate bimodal performance
46+
const queryResultCache = new Map();
47+
const QUERY_CACHE_TTL = 10 * 60 * 1000; // 10 minutes for query results
48+
49+
/**
50+
* Generate a cache key for a query
51+
* @param {string} collection - Collection name
52+
* @param {Object} filters - Query filters
53+
* @returns {string} - Cache key
54+
*/
55+
const generateQueryCacheKey = (collection, filters) => {
56+
return `${collection}:${JSON.stringify(filters)}`;
57+
};
58+
59+
/**
60+
* Get cached query result if available and not expired
61+
* @param {string} cacheKey - Cache key
62+
* @returns {Array|null} - Cached result or null
63+
*/
64+
const getCachedQueryResult = (cacheKey) => {
65+
const cached = queryResultCache.get(cacheKey);
66+
if (cached && (Date.now() - cached.timestamp) < QUERY_CACHE_TTL) {
67+
return cached.data;
68+
}
69+
return null;
70+
};
71+
72+
/**
73+
* Cache a query result
74+
* @param {string} cacheKey - Cache key
75+
* @param {Array} data - Query result data
76+
*/
77+
const setCachedQueryResult = (cacheKey, data) => {
78+
queryResultCache.set(cacheKey, {
79+
data: data,
80+
timestamp: Date.now()
81+
});
82+
};
83+
4584
/**
4685
* Get the latest date from a collection with caching
4786
* @param {Object} firestore - Firestore instance
@@ -209,5 +248,8 @@ export {
209248
preprocessParams,
210249
applyArrayFilter,
211250
selectFields,
212-
handleControllerError
251+
handleControllerError,
252+
generateQueryCacheKey,
253+
getCachedQueryResult,
254+
setCachedQueryResult
213255
};

src/utils/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ const createErrorResponse = (errors) => {
4545
};
4646
};
4747

48-
export { convertToArray, convertToHashes, createSuccessResponse, createErrorResponse };
48+
export { convertToArray, createSuccessResponse, createErrorResponse };

0 commit comments

Comments
 (0)