Skip to content

Commit 906ce07

Browse files
authored
Merge pull request #1152 from PathwayCommons/iss1151_img-cache
Image cache: Don't load doc
2 parents 28d70a6 + 5b38328 commit 906ce07

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/server/routes/api/document/index.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ const mapToUniprotIds = biopaxTemplate => {
224224
if ( dbPrefix !== 'ncbigene' ){
225225
return Promise.resolve();
226226
}
227-
227+
228228
const UNIPROT_DB_PREFIX = 'uniprot';
229229
const opts = {
230230
id: [
@@ -1441,26 +1441,32 @@ http.get('/(:id).png', function( req, res, next ){
14411441

14421442
res.setHeader('content-type', 'image/png');
14431443

1444-
const fillCache = async (doc, lastEditedDate) => {
1444+
const calcTtl = doc => {
1445+
const now = Date.now();
1446+
const lastEditedDate = doc.lastEditedDate();
1447+
return now - lastEditedDate;
1448+
};
1449+
1450+
const fillCache = async doc => {
14451451
const img = await getDocumentImageBuffer(doc);
1446-
const cache = { img, lastEditedDate };
1452+
const cache = { img };
1453+
const ttl = calcTtl(doc);
14471454

1448-
imageCache.set(id, cache);
1455+
imageCache.set(id, cache, {ttl});
14491456

14501457
return cache;
14511458
};
14521459

14531460
const main = async () => {
14541461
try {
1455-
const doc = await getDoc(id);
1456-
const lastEditedDate = '' + doc.lastEditedDate();
1457-
const cache = imageCache.get(id);
1458-
const canUseCache = imageCache.has(id) && cache.lastEditedDate === lastEditedDate;
1462+
const canUseCache = imageCache.has(id);
14591463

14601464
if( canUseCache ){
1465+
const cache = imageCache.get(id);
14611466
res.send(cache.img);
14621467
} else {
1463-
const cache = await fillCache(doc, lastEditedDate);
1468+
const doc = await getDoc(id);
1469+
const cache = await fillCache(doc);
14641470

14651471
res.send(cache.img);
14661472
}

0 commit comments

Comments
 (0)