From 260b263e6f4a46227fc4c196b8e31e371b300ca3 Mon Sep 17 00:00:00 2001 From: argirok Date: Wed, 3 Apr 2024 10:20:58 +0300 Subject: [PATCH] [develop | DONE | CHANGED] cache.js add compression library, reload content based on forceReload parameter or cache control headers --- services/cache/mecache/cache.js | 21 +++++++++++++++------ services/cache/mecache/package.json | 1 + 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/services/cache/mecache/cache.js b/services/cache/mecache/cache.js index a525742e..18a34898 100644 --- a/services/cache/mecache/cache.js +++ b/services/cache/mecache/cache.js @@ -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; } diff --git a/services/cache/mecache/package.json b/services/cache/mecache/package.json index c5b9a32e..f35341f4 100644 --- a/services/cache/mecache/package.json +++ b/services/cache/mecache/package.json @@ -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",