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