Skip to content

Commit e644731

Browse files
dianeCdrPixyannbertrander-lim
committed
feat(api): memoize Module version
Co-authored-by: Yann Bertrand <[email protected]> Co-authored-by: Eric Lim <[email protected]>
1 parent 2bb626d commit e644731

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

api/src/devcomp/infrastructure/repositories/module-repository.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { NotFoundError } from '../../../shared/domain/errors.js';
44
import { LearningContentResourceNotFound } from '../../../shared/domain/errors.js';
55
import { ModuleFactory } from '../factories/module-factory.js';
66

7+
const memoizedModuleVersions = new Map();
8+
79
async function getBySlug({ slug, moduleDatasource }) {
810
try {
911
const moduleData = await moduleDatasource.getBySlug(slug);
@@ -24,9 +26,13 @@ async function list({ moduleDatasource }) {
2426
}
2527

2628
function _computeModuleVersion(moduleData) {
27-
const hash = crypto.createHash('sha256');
28-
hash.update(JSON.stringify(moduleData));
29-
return hash.copy().digest('hex');
29+
if (!memoizedModuleVersions.has(moduleData.slug)) {
30+
const hash = crypto.createHash('sha256');
31+
hash.update(JSON.stringify(moduleData));
32+
const version = hash.copy().digest('hex');
33+
memoizedModuleVersions.set(moduleData.slug, version);
34+
}
35+
return memoizedModuleVersions.get(moduleData.slug);
3036
}
3137

3238
export { getBySlug, list };

0 commit comments

Comments
 (0)