Add 4-hour caching for all generated images

This commit is contained in:
goat 2021-05-26 16:41:51 +02:00
parent 026b056e7a
commit 341236e170
No known key found for this signature in database
GPG key ID: F18F057873895461
4 changed files with 159 additions and 27 deletions

2
.gitignore vendored
View file

@ -102,3 +102,5 @@ dist
# TernJS port file # TernJS port file
.tern-port .tern-port
diskcache/

View file

@ -8,19 +8,70 @@ const { CardCreator } = require('./create-card');
const creator = new CardCreator(); const creator = new CardCreator();
creator.init(); creator.init();
app.get('/characters/id/:charaId.png', async (req, res) => { // node cachemanager
try { var cacheManager = require('cache-manager');
const png = await creator.createCard(req.params.charaId); // storage for the cachemanager
var fsStore = require('cache-manager-fs-binary');
res.writeHead(200, { // initialize caching on disk
'Content-Type': 'image/png', var diskCache = cacheManager.caching({
'Content-Length': png.length, store: fsStore,
'Cache-Control': 'public, max-age=14400' options: {
}); reviveBuffers: true,
res.end(png, 'binary'); binaryAsStream: false,
}catch(error){ ttl: 60 * 60 * 4 /* seconds */,
res.send(error); maxsize: 1000 * 1000 * 1000 /* max size in bytes on disk */,
path: 'diskcache',
preventfill: true
} }
});
app.get('/characters/id/:charaId.png', async (req, res) => {
var cacheKey = `img:${req.params.charaId}`;
var ttl = 60 * 60 * 4; // 4 hours
diskCache.wrap(cacheKey,
// called if the cache misses in order to generate the value to cache
function (cb) {
creator.createCard(req.params.charaId).then(image => cb(null, {
binary: {
image: image,
}
})).catch((reason) => cb(reason, null));
},
// Options, see node-cache-manager for more examples
{ ttl: ttl },
function (err, result) {
if (err !== null) {
res.status(500).send("Lodestone did not respond in time.");
return;
}
var image = result.binary.image;
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': image.length,
'Cache-Control': 'public, max-age=14400'
});
res.end(image, 'binary');
var usedStreams = ['image'];
// you have to do the work to close the unused files
// to prevent file descriptors leak
for (var key in result.binary) {
if (!result.binary.hasOwnProperty(key)) continue;
if (usedStreams.indexOf(key) < 0
&& result.binary[key] instanceof Stream.Readable) {
if (typeof result.binary[key].close === 'function') {
result.binary[key].close(); // close the stream (fs has it)
} else {
result.binary[key].resume(); // resume to the end and close
}
}
}
}
);
}) })
app.get('/characters/id/:charaId', async (req, res) => { app.get('/characters/id/:charaId', async (req, res) => {
@ -30,25 +81,13 @@ app.get('/characters/id/:charaId', async (req, res) => {
app.get('/characters/name/:world/:charName.png', async (req, res) => { app.get('/characters/name/:world/:charName.png', async (req, res) => {
var response = await fetch(`https://xivapi.com/character/search?name=${req.params.charName}&server=${req.params.world}`); var response = await fetch(`https://xivapi.com/character/search?name=${req.params.charName}&server=${req.params.world}`);
var data = await response.json(); var data = await response.json();
if (data.Results[0] === undefined) if (data.Results[0] === undefined) {
{
res.status(404).send("Character not found."); res.status(404).send("Character not found.");
return; return;
} }
try { res.redirect(`/characters/id/${data.Results[0].ID}.png`);
const png = await creator.createCard(data.Results[0].ID);
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': png.length,
'Cache-Control': 'public, max-age=14400'
});
res.end(png, 'binary');
}catch(error){
res.send(error);
}
}) })
app.get('/characters/name/:world/:charName', async (req, res) => { app.get('/characters/name/:world/:charName', async (req, res) => {

89
package-lock.json generated
View file

@ -106,6 +106,11 @@
"color-convert": "^2.0.1" "color-convert": "^2.0.1"
} }
}, },
"any-promise": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/any-promise/-/any-promise-0.1.0.tgz",
"integrity": "sha1-gwtoCqflbzNFHUsEnzvYBESY7ic="
},
"anymatch": { "anymatch": {
"version": "3.1.2", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.2.tgz", "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.2.tgz",
@ -134,6 +139,11 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
}, },
"async": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz",
"integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo="
},
"balanced-match": { "balanced-match": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
@ -248,6 +258,57 @@
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
"integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg==" "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
}, },
"cache-manager": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/cache-manager/-/cache-manager-1.5.0.tgz",
"integrity": "sha1-UpIUvaV/oZUU0QbwcPq7FsMZCBE=",
"requires": {
"async": "^1.5.2",
"lru-cache": "4.0.0"
},
"dependencies": {
"lru-cache": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.0.0.tgz",
"integrity": "sha1-tcvwFVbBaWb+vlTO7A+03JDfbCg=",
"requires": {
"pseudomap": "^1.0.1",
"yallist": "^2.0.0"
}
},
"yallist": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
"integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI="
}
}
},
"cache-manager-fs": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/cache-manager-fs/-/cache-manager-fs-1.0.8.tgz",
"integrity": "sha512-Rrvd5N5DC9Gh6SPSB9A8f/FfPOuHkDL1ig+oSut8iAcYPda8xHqf9OEr3v4vKNYI3svYSoZ+uBqDJctLMRvqiA==",
"requires": {
"async": "^1.4.2",
"cache-manager": "^1.1.0",
"extend": "^3.0.0",
"fs-promise": "^0.3.1",
"uuid": "^2.0.1"
}
},
"cache-manager-fs-binary": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/cache-manager-fs-binary/-/cache-manager-fs-binary-1.0.4.tgz",
"integrity": "sha1-7/0HWWZkSdvwFGB6SOolNsvetXY=",
"requires": {
"async": "^1.4.2",
"cache-manager": "^1.1.0",
"extend": "^3.0.0",
"fs-promise": "^0.3.1",
"glob": "^7.0.3",
"streamifier": "^0.1.1",
"uuid": "^2.0.1"
}
},
"cacheable-request": { "cacheable-request": {
"version": "6.1.0", "version": "6.1.0",
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-6.1.0.tgz", "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-6.1.0.tgz",
@ -584,6 +645,11 @@
} }
} }
}, },
"extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
"integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="
},
"fill-range": { "fill-range": {
"version": "7.0.1", "version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
@ -639,6 +705,14 @@
"minipass": "^3.0.0" "minipass": "^3.0.0"
} }
}, },
"fs-promise": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/fs-promise/-/fs-promise-0.3.1.tgz",
"integrity": "sha1-vzQFA2jyTW3J38ZoirXOrY+GhCo=",
"requires": {
"any-promise": "~0.1.0"
}
},
"fs.realpath": { "fs.realpath": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@ -1207,6 +1281,11 @@
"ipaddr.js": "1.9.1" "ipaddr.js": "1.9.1"
} }
}, },
"pseudomap": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
"integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM="
},
"pstree.remy": { "pstree.remy": {
"version": "1.1.8", "version": "1.1.8",
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
@ -1436,6 +1515,11 @@
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
}, },
"streamifier": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/streamifier/-/streamifier-0.1.1.tgz",
"integrity": "sha1-l+mNj6TRBdYqJpHR3AfoINuN/E8="
},
"string-width": { "string-width": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
@ -1625,6 +1709,11 @@
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
}, },
"uuid": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.3.tgz",
"integrity": "sha1-Z+LoY3lyFVMN/zGOW/nc6/1Hsho="
},
"vary": { "vary": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",

View file

@ -8,6 +8,8 @@
"dev": "nodemon index.js" "dev": "nodemon index.js"
}, },
"dependencies": { "dependencies": {
"cache-manager-fs": "^1.0.8",
"cache-manager-fs-binary": "^1.0.4",
"canvas": "^2.6.1", "canvas": "^2.6.1",
"express": "^4.17.1", "express": "^4.17.1",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",