Refactored caching utility: replaced jsonToCacheKey with getQueryToCacheKey for improved cache key generation based on model, id, and populate parameters.

This commit is contained in:
Tom Butcher 2025-12-13 21:07:29 +00:00
parent 94e0614852
commit f6e07e2cca

View File

@ -76,8 +76,14 @@ export function getModelByName(modelName) {
return modelList.filter(model => model.modelName == modelName)[0];
}
export function jsonToCacheKey(obj) {
const normalized = canonicalize(obj);
const hash = crypto.createHash('sha256').update(normalized).digest('hex');
return hash;
export function getQueryToCacheKey({ model, id, populate }) {
const populateKey = [];
for (const pop of populate) {
if (typeof pop === 'string') {
populateKey.push(pop);
} else if (typeof pop === 'object') {
populateKey.push(pop.path);
}
}
return `${model}:${id?.toString()}-${populateKey.join(',')}`;
}