Skip to content

Commit 6c1f35b

Browse files
Changes as per review
LF-3081
1 parent 3ab8347 commit 6c1f35b

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

src/sdc-ig-supplements.js

+71-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,72 @@ engine.weight = function (coll) {
8989
return hasPromise ? Promise.all(res) : res;
9090
};
9191

92+
// Object for storing received scores
9293
const weightCache = {};
94+
// Object for storing fetch promises.
95+
const requestCache = {};
96+
// Duration of data storage in cache.
97+
const cacheStorageTime = 3600000; // 1 hour = 60 * 60 * 1000
98+
99+
/**
100+
* Caches score.
101+
* @param {string} key - key to store score in cache.
102+
* @param {number|Promise} value - value of score or promise of value.
103+
*/
104+
function putScoreToCache(key, value) {
105+
weightCache[key] = {
106+
timestamp: Date.now(),
107+
value
108+
};
109+
}
110+
111+
/**
112+
* Checks if there is an unexpired score in the cache.
113+
* @param {string} key - key to store score in cache.
114+
* @return {boolean|undefined}
115+
*/
116+
function hasScoreInCache(key) {
117+
return weightCache[key] && Date.now() - weightCache[key].timestamp < cacheStorageTime;
118+
}
119+
120+
/**
121+
* Returns a score or promise of score from the cache.
122+
* @param {string} key - key to store score in cache.
123+
* @return {number | Promise}
124+
*/
125+
function getScoreFromCache(key) {
126+
return weightCache[key].value;
127+
}
128+
129+
/**
130+
* fetch() wrapper for caching server responses.
131+
* @param {string} url - a URL of the resource you want to fetch.
132+
* @param {object} [options] - optional object containing any custom settings
133+
* that you want to apply to the request.
134+
* @return {Promise}
135+
*/
136+
function fetchWithCache(url, options) {
137+
const requestKey = [
138+
url + (options ? JSON.stringify(options) : '')
139+
].join('|');
140+
141+
const timestamp = Date.now();
142+
for (const key in requestCache) {
143+
if (timestamp - requestCache[key].timestamp > cacheStorageTime) {
144+
// Remove responses older than an hour
145+
delete requestCache[key];
146+
}
147+
}
148+
149+
if (!requestCache[requestKey]) {
150+
requestCache[requestKey] = {
151+
timestamp,
152+
promise: options ? fetch(url, options) : fetch(url)
153+
};
154+
}
155+
156+
return requestCache[requestKey].promise;
157+
}
93158

94159
/**
95160
* Adds the value of score or its promise received from a corresponding value
@@ -110,8 +175,8 @@ function addWeightFromCorrespondingResourcesToResult(res, ctx, questionnaire, vs
110175
questionnaire?.url || questionnaire?.id, vsURL, code, system
111176
].join('|');
112177

113-
if (Object.prototype.hasOwnProperty.call(weightCache, cacheKey)) {
114-
score = weightCache[cacheKey];
178+
if (hasScoreInCache(cacheKey)) {
179+
score = getScoreFromCache(cacheKey);
115180
} else {
116181
if (code) {
117182
if (vsURL) {
@@ -124,7 +189,7 @@ function addWeightFromCorrespondingResourcesToResult(res, ctx, questionnaire, vs
124189

125190
if (containedVS) {
126191
if (!containedVS.expansion) {
127-
score = fetch(`${getTerminologyUrl(ctx)}/ValueSet/$expand`, {
192+
score = fetchWithCache(`${getTerminologyUrl(ctx)}/ValueSet/$expand`, {
128193
method: 'POST',
129194
headers: {
130195
'Accept': 'application/fhir+json',
@@ -153,7 +218,7 @@ function addWeightFromCorrespondingResourcesToResult(res, ctx, questionnaire, vs
153218
);
154219
}
155220
} else {
156-
score = fetch(`${getTerminologyUrl(ctx)}/ValueSet/$expand?` + new URLSearchParams({
221+
score = fetchWithCache(`${getTerminologyUrl(ctx)}/ValueSet/$expand?` + new URLSearchParams({
157222
url: vsURL,
158223
property: 'itemWeight'
159224
}, {
@@ -195,7 +260,7 @@ function addWeightFromCorrespondingResourcesToResult(res, ctx, questionnaire, vs
195260
}
196261
}
197262

198-
weightCache[cacheKey] = score;
263+
putScoreToCache(cacheKey, score);
199264
}
200265

201266
if (score !== undefined) {
@@ -216,7 +281,7 @@ function addWeightFromCorrespondingResourcesToResult(res, ctx, questionnaire, vs
216281
* @return {Promise<number|undefined>}
217282
*/
218283
function getWeightFromTerminologyCodeSet(ctx, code, system) {
219-
return fetch(`${getTerminologyUrl(ctx)}/CodeSystem/$lookup?` + new URLSearchParams({
284+
return fetchWithCache(`${getTerminologyUrl(ctx)}/CodeSystem/$lookup?` + new URLSearchParams({
220285
code, system, property: 'itemWeight'
221286
}, {
222287
headers: {

test/supplements.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,11 @@ describe("supplements", () => {
484484
}]
485485
}, {
486486
"code": "some-code-2",
487-
"system": "some-system-2"
487+
"system": "some-system-10"
488488
}]
489489
}
490490
}],
491-
['/CodeSystem/$lookup?code=some-code-2&system=some-system-2&property=itemWeight',
491+
['/CodeSystem/$lookup?code=some-code-2&system=some-system-10&property=itemWeight',
492492
null,
493493
{
494494
"resourceType": "OperationOutcome",
@@ -526,7 +526,7 @@ describe("supplements", () => {
526526
{
527527
"valueCoding": {
528528
"code": "some-code-2",
529-
"system": "some-system-2"
529+
"system": "some-system-10"
530530
}
531531
}
532532
]
@@ -809,7 +809,7 @@ describe("supplements", () => {
809809

810810
it("should return the correct result when getting scores from a contained value set and a code system from terminology server", (done) => {
811811
mockFetchResults([
812-
['/CodeSystem/$lookup?code=some-code-2&system=some-system-2&property=itemWeight',
812+
['/CodeSystem/$lookup?code=some-code-2&system=some-system-20&property=itemWeight',
813813
{
814814
"resourceType": "Parameters",
815815
"parameter": [ {
@@ -851,7 +851,7 @@ describe("supplements", () => {
851851
{
852852
"valueCoding": {
853853
"code": "some-code-2",
854-
"system": "some-system-2"
854+
"system": "some-system-20"
855855
}
856856
}
857857
]
@@ -888,7 +888,7 @@ describe("supplements", () => {
888888
}]
889889
}, {
890890
"code": "some-code-2",
891-
"system": "some-system-2",
891+
"system": "some-system-20",
892892
"contains": [{
893893
"code": "some-code-2",
894894
"system": "some-system-1",

0 commit comments

Comments
 (0)