Cache service:

-change console.debug to log
-mv resetAtMidnight inside app.listen


git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@59002 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
argiro.kokogiannaki 2020-06-26 14:44:28 +00:00
parent a9652670b3
commit cac44d718e
1 changed files with 6 additions and 27 deletions

View File

@ -41,7 +41,7 @@ let cache = () => {
return (req, res, next) => {
if(req.query.url) {
if(mcache.size() > cacheMaxSize){
console.debug("Max cache size reached!"+cacheMaxSize);
console.log("Max cache size reached!"+cacheMaxSize);
clearCache();
}
let key = '__express__' + req.query.url;
@ -129,33 +129,14 @@ app.get('/get', cache(), cors(), (req, res) => {
});
app.get('/get', cache(30), 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.use((req, res) => {
res.status(404).send(getResponse(404, "Not Found")); //not found
});
app.listen((process.env.PORT) ? process.env.PORT : 3000, function () {
console.log(`Example app listening on port ${process.env.PORT}!`)
console.log(`Example app listening on port ${(process.env.PORT) ? process.env.PORT : 3000}!`)
//run the timer
resetAtMidnight();
});
function getResponse(code, message) {
@ -166,7 +147,7 @@ function getResponse(code, message) {
}
function clearCache(){
console.debug("cache is cleared!");
console.log("cache is cleared!");
mcache.clear();
entries.set(mcache.size());
}
@ -179,10 +160,8 @@ function checkForLongCachedRequests(url){
});
return long;
}
//run the timer
resetAtMidnight();
function resetAtMidnight() {
console.debug("Run Reset timer");
console.log("Run Reset timer");
var now = new Date();
var night = new Date(
now.getFullYear(),