[develop | DONE | CHANGED] cache.js add compression library, reload content based on forceReload parameter or cache control headers
This commit is contained in:
parent
d42a3973bd
commit
260b263e6f
|
@ -6,6 +6,7 @@ const { LRUCache } = require('lru-cache');
|
|||
const request = require('superagent');
|
||||
const prom = require('prom-client');
|
||||
const URL = require('url');
|
||||
let compression = require("compression");
|
||||
const PropertiesReader = require('properties-reader');
|
||||
const properties = PropertiesReader('./properties.file');
|
||||
const environment = properties.get("environment");
|
||||
|
@ -14,6 +15,7 @@ const preloadRequests = properties.get("preloadRequests").split(',');;
|
|||
const cacheMaxSize = 1000;
|
||||
let cors = require('cors');
|
||||
app.use(cors());
|
||||
app.use(compression());
|
||||
const lruCache = new LRUCache({ max: cacheMaxSize });
|
||||
const register = new prom.Registry();
|
||||
prom.collectDefaultMetrics({register: register});
|
||||
|
@ -42,18 +44,25 @@ const histogram = new prom.Histogram({
|
|||
let cache = () => {
|
||||
return (req, res, next) => {
|
||||
if (req.query.url) {
|
||||
let key = '__express__' + req.query.url;
|
||||
let key = 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 scheme = url.protocol.replace(':', '');
|
||||
if (lruCache.has(key)) {
|
||||
// console.log( key, "hit")
|
||||
if (lruCache.has(key) && !forceReload) {
|
||||
// console.log( key, "hit")
|
||||
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'});
|
||||
res.send(JSON.parse(lruCache.get(key)));
|
||||
responses.inc({scheme: scheme, target: target, code: res.statusCode});
|
||||
end();
|
||||
} else {
|
||||
// console.log( key, "miss")
|
||||
// console.log( key, "miss", forceReload)
|
||||
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'miss'});
|
||||
res.sendResponse = res.send;
|
||||
res.send = (body) => {
|
||||
|
@ -85,9 +94,9 @@ let cache = () => {
|
|||
};
|
||||
function isAllowedToBeCached(scheme, target){
|
||||
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"){
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.6.7",
|
||||
"compression": "^1.7.4",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"lru-cache": "^10.2.0",
|
||||
|
|
Loading…
Reference in New Issue