Revision 61402
Added by Konstantinos Triantafyllou almost 3 years ago
cache.js | ||
---|---|---|
8 | 8 |
const URL = require('url'); |
9 | 9 |
const expireShort = 2 * 60 * 1000; //2mins |
10 | 10 |
const expireLong = 24 * 60 * 60 * 1000; //24 hours |
11 |
const cacheMaxSize =500; |
|
11 |
const cacheMaxSize = 500;
|
|
12 | 12 |
const longCachingRequests = ["/communityFull", "/full", "/pagehelpcontent", |
13 |
"/provision/mvc/vocabularies/","/pages?page_route=",
|
|
14 |
"/allmetrics", "/countryusagestats/", "/openaire/info", |
|
15 |
"/api/communities/", "/openaire/contexts/"]; |
|
13 |
"/provision/mvc/vocabularies/", "/pages?page_route=",
|
|
14 |
"/allmetrics", "/countryusagestats/", "/openaire/info",
|
|
15 |
"/api/communities/", "/openaire/contexts/"];
|
|
16 | 16 |
let cors = require('cors'); |
17 | 17 |
app.use(cors()); |
18 | 18 |
|
... | ... | |
42 | 42 |
|
43 | 43 |
let cache = () => { |
44 | 44 |
return (req, res, next) => { |
45 |
if(req.query.url) { |
|
46 |
if(mcache.memsize() > cacheMaxSize){
|
|
47 |
console.log("Max cache size reached!"+cacheMaxSize);
|
|
45 |
if (req.query.url) {
|
|
46 |
if (mcache.memsize() > cacheMaxSize) {
|
|
47 |
console.log("Max cache size reached!" + cacheMaxSize);
|
|
48 | 48 |
clearCache(); |
49 | 49 |
} |
50 | 50 |
let key = '__express__' + req.query.url; |
... | ... | |
64 | 64 |
res.send = (body) => { |
65 | 65 |
let alreadyCached = !!mcache.get(key); |
66 | 66 |
entries.set(mcache.memsize()); |
67 |
if(!alreadyCached) { |
|
67 |
if (!alreadyCached) {
|
|
68 | 68 |
responses.inc({scheme: scheme, target: target, code: res.statusCode}); |
69 | 69 |
end(); |
70 | 70 |
} |
71 | 71 |
if (res.statusCode === 200) { |
72 | 72 |
// console.log("Expire in " +(longCache?expireLong:expireShort) + " " +req.query.url); |
73 |
mcache.put(key, body,longCache?expireLong:expireShort, () => {
|
|
73 |
mcache.put(key, body, longCache ? expireLong : expireShort, () => {
|
|
74 | 74 |
entries.set(mcache.memsize()); |
75 | 75 |
}); |
76 | 76 |
} |
... | ... | |
87 | 87 |
app.get('/clear', (req, res) => { |
88 | 88 |
let c = mcache.size(); |
89 | 89 |
const url = req.query.url; |
90 |
let message =""; |
|
91 |
if(url){
|
|
90 |
let message = "";
|
|
91 |
if (url) {
|
|
92 | 92 |
let key = '__express__' + req.query.url; |
93 | 93 |
mcache.del(key); |
94 |
message = "Delete entry with key "+ url; |
|
94 |
message = "Delete entry with key " + url;
|
|
95 | 95 |
entries.set(mcache.size()); |
96 |
}else{
|
|
96 |
} else {
|
|
97 | 97 |
clearCache(); |
98 |
message ="Delete "+c + " entries. Now there are: "+mcache.size()
|
|
99 |
}
|
|
98 |
message = "Delete " + c + " entries. Now there are: " + mcache.size()
|
|
99 |
} |
|
100 | 100 |
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length"); |
101 | 101 |
res.header("Access-Control-Allow-Methods", "GET, OPTIONS"); |
102 | 102 |
res.header("Access-Control-Allow-Methods", "GET, OPTIONS"); |
103 | 103 |
res.header("Content-Type", "application/json"); |
104 |
res.status(200).send(getResponse(200,message)); |
|
104 |
res.status(200).send(getResponse(200, message));
|
|
105 | 105 |
|
106 | 106 |
}); |
107 | 107 |
app.get('/metrics', (req, res) => { |
... | ... | |
147 | 147 |
response["code"] = code; |
148 | 148 |
response["message"] = message; |
149 | 149 |
return response; |
150 |
} |
|
150 | 151 |
|
151 |
} |
|
152 |
function clearCache(){ |
|
152 |
function clearCache() { |
|
153 | 153 |
console.log("cache is cleared!"); |
154 | 154 |
mcache.clear(); |
155 | 155 |
entries.set(mcache.size()); |
156 | 156 |
} |
157 |
function checkForLongCachedRequests(url){ |
|
158 |
let long =false; |
|
157 |
|
|
158 |
function checkForLongCachedRequests(url) { |
|
159 |
let long = false; |
|
159 | 160 |
longCachingRequests.forEach(partUrl => { |
160 |
if(url.indexOf(partUrl) !==-1){
|
|
161 |
if (url.indexOf(partUrl) !== -1) {
|
|
161 | 162 |
long = true; |
162 | 163 |
} |
163 | 164 |
}); |
164 | 165 |
return long; |
165 | 166 |
} |
167 |
|
|
166 | 168 |
function resetAtMidnight() { |
167 | 169 |
console.log("Run Reset timer"); |
168 | 170 |
var now = new Date(); |
169 | 171 |
var night = new Date( |
170 | 172 |
now.getFullYear(), |
171 | 173 |
now.getMonth(), |
172 |
now.getDate() + 1 , // the next day, ...
|
|
173 |
0, 0, 0 // ...at 00:00:00 hours
|
|
174 |
now.getDate() + 1, // the next day, ... |
|
175 |
0, 0, 0 // ...at 00:00:00 hours |
|
174 | 176 |
); |
175 | 177 |
var msToMidnight = night.getTime() - now.getTime(); |
176 |
|
|
177 |
setTimeout(function() { |
|
178 |
setTimeout(function () { |
|
178 | 179 |
clearCache(); // <-- This is the function being called at midnight. |
179 | 180 |
resetAtMidnight(); // Then, reset again next midnight. |
180 | 181 |
}, msToMidnight); |
Also available in: Unified diff
[Explore-Service]: Update to angular 11. Delete branches