[Cahce service]
Add: const expireShort = 2 * 60 * 1000; //2mins const expireLong = 24 * 60 * 60 * 1000; //24 hours const cacheMaxSize =500; const longCachingRequests = ["/communityFull/", "/pagehelpcontent","/provision/mvc/vocabularies/","/pages?page_route="]; -use expireLongTime for longCachingRequests -add /clear request -add timer to clean the cache on midnight -when maxcachesize is reached clean the cache git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@59001 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
7bc0767518
commit
a9652670b3
|
@ -6,7 +6,10 @@ let mcache = require('memory-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');
|
||||||
|
const expireShort = 2 * 60 * 1000; //2mins
|
||||||
|
const expireLong = 24 * 60 * 60 * 1000; //24 hours
|
||||||
|
const cacheMaxSize =500;
|
||||||
|
const longCachingRequests = ["/communityFull/", "/pagehelpcontent","/provision/mvc/vocabularies/","/pages?page_route="];
|
||||||
let cors = require('cors');
|
let cors = require('cors');
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
|
@ -34,10 +37,15 @@ const histogram = new prom.Histogram({
|
||||||
buckets: [0.1, 0.2, 0.5, 1, 2]
|
buckets: [0.1, 0.2, 0.5, 1, 2]
|
||||||
});
|
});
|
||||||
|
|
||||||
let cache = (duration) => {
|
let cache = () => {
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
if(req.query.url) {
|
if(req.query.url) {
|
||||||
|
if(mcache.size() > cacheMaxSize){
|
||||||
|
console.debug("Max cache size reached!"+cacheMaxSize);
|
||||||
|
clearCache();
|
||||||
|
}
|
||||||
let key = '__express__' + req.query.url;
|
let key = '__express__' + req.query.url;
|
||||||
|
let longCache = checkForLongCachedRequests(req.query.url);
|
||||||
let cachedBody = mcache.get(key);
|
let cachedBody = mcache.get(key);
|
||||||
const url = new URL.parse(req.query.url);
|
const url = new URL.parse(req.query.url);
|
||||||
const target = url.host + '/' + url.pathname.split('/')[1];
|
const target = url.host + '/' + url.pathname.split('/')[1];
|
||||||
|
@ -58,7 +66,8 @@ let cache = (duration) => {
|
||||||
end();
|
end();
|
||||||
}
|
}
|
||||||
if (res.statusCode === 200) {
|
if (res.statusCode === 200) {
|
||||||
mcache.put(key, body, duration * 1000, () => {
|
// console.log("Expire in " +(longCache?expireLong:expireShort) + " " +req.query.url);
|
||||||
|
mcache.put(key, body,longCache?expireLong:expireShort, () => {
|
||||||
entries.set(mcache.size());
|
entries.set(mcache.size());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -72,12 +81,54 @@ let cache = (duration) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
app.get('/clear', (req, res) => {
|
||||||
|
let c = mcache.size();
|
||||||
|
const url = req.query.url;
|
||||||
|
let message ="";
|
||||||
|
if(url){
|
||||||
|
let key = '__express__' + req.query.url;
|
||||||
|
mcache.del(key);
|
||||||
|
message = "Delete entry with key "+ url;
|
||||||
|
entries.set(mcache.size());
|
||||||
|
}else{
|
||||||
|
clearCache();
|
||||||
|
message ="Delete "+c + " entries. Now there are: "+mcache.size()
|
||||||
|
}
|
||||||
|
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length");
|
||||||
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||||
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||||
|
res.header("Content-Type", "application/json");
|
||||||
|
res.status(200).send(getResponse(200,message));
|
||||||
|
|
||||||
|
});
|
||||||
app.get('/metrics', (req, res) => {
|
app.get('/metrics', (req, res) => {
|
||||||
res.set('Content-Type', register.contentType);
|
res.set('Content-Type', register.contentType);
|
||||||
res.end(register.metrics());
|
res.end(register.metrics());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/get', cache(), cors(), (req, res) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
const url = (req.query) ? req.query.url : null;
|
||||||
|
if (!url) {
|
||||||
|
res.status(404).send(getResponse(404, "Not Found ")) //not found
|
||||||
|
} else {
|
||||||
|
request.get(url, function (err, response) {
|
||||||
|
// res.header("Access-Control-Allow-Origin", "http://localhost:3000");
|
||||||
|
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length");
|
||||||
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||||
|
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||||
|
res.header("Content-Type", "application/json");
|
||||||
|
if (!response && err) {
|
||||||
|
res.status(500).send(getResponse(500, "An error occurred for " + url))
|
||||||
|
} else {
|
||||||
|
res.status(response.status).send(response.body);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get('/get', cache(30), cors(), (req, res) => {
|
app.get('/get', cache(30), cors(), (req, res) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const url = (req.query) ? req.query.url : null;
|
const url = (req.query) ? req.query.url : null;
|
||||||
|
@ -99,7 +150,6 @@ app.get('/get', cache(30), cors(), (req, res) => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
res.status(404).send(getResponse(404, "Not Found")); //not found
|
res.status(404).send(getResponse(404, "Not Found")); //not found
|
||||||
});
|
});
|
||||||
|
@ -115,3 +165,35 @@ function getResponse(code, message) {
|
||||||
return response;
|
return response;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
function clearCache(){
|
||||||
|
console.debug("cache is cleared!");
|
||||||
|
mcache.clear();
|
||||||
|
entries.set(mcache.size());
|
||||||
|
}
|
||||||
|
function checkForLongCachedRequests(url){
|
||||||
|
let long =false;
|
||||||
|
longCachingRequests.forEach(partUrl => {
|
||||||
|
if(url.indexOf(partUrl) !==-1){
|
||||||
|
long = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return long;
|
||||||
|
}
|
||||||
|
//run the timer
|
||||||
|
resetAtMidnight();
|
||||||
|
function resetAtMidnight() {
|
||||||
|
console.debug("Run Reset timer");
|
||||||
|
var now = new Date();
|
||||||
|
var night = new Date(
|
||||||
|
now.getFullYear(),
|
||||||
|
now.getMonth(),
|
||||||
|
now.getDate() + 1 , // the next day, ...
|
||||||
|
0, 0, 0 // ...at 00:00:00 hours
|
||||||
|
);
|
||||||
|
var msToMidnight = night.getTime() - now.getTime();
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
clearCache(); // <-- This is the function being called at midnight.
|
||||||
|
resetAtMidnight(); // Then, reset again next midnight.
|
||||||
|
}, msToMidnight);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue