Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/server/routes/api/document/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const mapToUniprotIds = biopaxTemplate => {
if ( dbPrefix !== 'ncbigene' ){
return Promise.resolve();
}

const UNIPROT_DB_PREFIX = 'uniprot';
const opts = {
id: [
Expand Down Expand Up @@ -1441,26 +1441,32 @@ http.get('/(:id).png', function( req, res, next ){

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

const fillCache = async (doc, lastEditedDate) => {
const calcTtl = doc => {
const now = Date.now();
const lastEditedDate = doc.lastEditedDate();
return now - lastEditedDate;
};

const fillCache = async doc => {
const img = await getDocumentImageBuffer(doc);
const cache = { img, lastEditedDate };
const cache = { img };
const ttl = calcTtl(doc);

imageCache.set(id, cache);
imageCache.set(id, cache, {ttl});

return cache;
};

const main = async () => {
try {
const doc = await getDoc(id);
const lastEditedDate = '' + doc.lastEditedDate();
const cache = imageCache.get(id);
const canUseCache = imageCache.has(id) && cache.lastEditedDate === lastEditedDate;
const canUseCache = imageCache.has(id);

if( canUseCache ){
const cache = imageCache.get(id);
res.send(cache.img);
} else {
const cache = await fillCache(doc, lastEditedDate);
const doc = await getDoc(id);
const cache = await fillCache(doc);

res.send(cache.img);
}
Expand Down