[develop | DONE | CHANGED] cache.js add compression library, reload content based on forceReload parameter or cache control headers

This commit is contained in:
argirok 2024-04-03 10:20:58 +03:00
parent d42a3973bd
commit 260b263e6f
2 changed files with 16 additions and 6 deletions

View File

@ -6,6 +6,7 @@ const { LRUCache } = require('lru-cache');
const request = require('superagent'); const request = require('superagent');
const prom = require('prom-client'); const prom = require('prom-client');
const URL = require('url'); const URL = require('url');
let compression = require("compression");
const PropertiesReader = require('properties-reader'); const PropertiesReader = require('properties-reader');
const properties = PropertiesReader('./properties.file'); const properties = PropertiesReader('./properties.file');
const environment = properties.get("environment"); const environment = properties.get("environment");
@ -14,6 +15,7 @@ const preloadRequests = properties.get("preloadRequests").split(',');;
const cacheMaxSize = 1000; const cacheMaxSize = 1000;
let cors = require('cors'); let cors = require('cors');
app.use(cors()); app.use(cors());
app.use(compression());
const lruCache = new LRUCache({ max: cacheMaxSize }); const lruCache = new LRUCache({ max: cacheMaxSize });
const register = new prom.Registry(); const register = new prom.Registry();
prom.collectDefaultMetrics({register: register}); prom.collectDefaultMetrics({register: register});
@ -42,18 +44,25 @@ const histogram = new prom.Histogram({
let cache = () => { let cache = () => {
return (req, res, next) => { return (req, res, next) => {
if (req.query.url) { if (req.query.url) {
let key = '__express__' + req.query.url; let key = req.query.url;
const url = new URL.parse(req.query.url); const url = new URL.parse(req.query.url);
// console.log(req.headers.origin, req.headers.referrer, req.headers.origin)
const cacheControlHeader = req.headers['cache-control'];
// Log the Cache-Control header
// console.log('Cache-Control header sent by client:', cacheControlHeader);
let forceReload = req.query.forceReload && req.query.forceReload == 'true'?true:false;
forceReload = forceReload || (cacheControlHeader && (cacheControlHeader.indexOf("no-cache") || cacheControlHeader.indexOf("no-store") || cacheControlHeader.indexOf("must-revalidate")))?true:false;
const target = url.host + '/' + url.pathname.split('/')[1]; const target = url.host + '/' + url.pathname.split('/')[1];
const scheme = url.protocol.replace(':', ''); const scheme = url.protocol.replace(':', '');
if (lruCache.has(key)) { if (lruCache.has(key) && !forceReload) {
// console.log( key, "hit") // console.log( key, "hit")
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'}); const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'});
res.send(JSON.parse(lruCache.get(key))); res.send(JSON.parse(lruCache.get(key)));
responses.inc({scheme: scheme, target: target, code: res.statusCode}); responses.inc({scheme: scheme, target: target, code: res.statusCode});
end(); end();
} else { } else {
// console.log( key, "miss") // console.log( key, "miss", forceReload)
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'miss'}); const end = histogram.startTimer({scheme: scheme, target: target, cache: 'miss'});
res.sendResponse = res.send; res.sendResponse = res.send;
res.send = (body) => { res.send = (body) => {
@ -85,9 +94,9 @@ let cache = () => {
}; };
function isAllowedToBeCached(scheme, target){ function isAllowedToBeCached(scheme, target){
if(environment != "development"){ if(environment != "development"){
return scheme.indexOf("https")!=-1 && ( target.indexOf(".openaire.eu/") !=-1 || target.indexOf(".zenodo.org/api/") !=-1 || target.indexOf("lab.idiap.ch/enermaps" != -1)) return scheme.indexOf("https")!=-1 && ( target.indexOf(".openaire.eu/") !=-1 || target.indexOf("zenodo.org/api") !=-1 || target.indexOf("lab.idiap.ch/enermaps" != -1))
} else if(environment == "development"){ } else if(environment == "development"){
return target.indexOf(".openaire.eu/") !=-1 || target.indexOf(".di.uoa.gr") !=-1 || target.indexOf("dev-openaire.d4science.org") !=-1 || target.indexOf("lab.idiap.ch/enermaps") != -1 return target.indexOf(".openaire.eu/") !=-1 || target.indexOf(".di.uoa.gr") !=-1 || target.indexOf("zenodo.org/api") !=-1 || target.indexOf("dev-openaire.d4science.org") !=-1 || target.indexOf("lab.idiap.ch/enermaps") != -1
} }
return true; return true;
} }

View File

@ -11,6 +11,7 @@
}, },
"dependencies": { "dependencies": {
"axios": "^1.6.7", "axios": "^1.6.7",
"compression": "^1.7.4",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
"lru-cache": "^10.2.0", "lru-cache": "^10.2.0",