-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprerender-memory-cache.js
More file actions
104 lines (93 loc) · 2.7 KB
/
Copy pathprerender-memory-cache.js
File metadata and controls
104 lines (93 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var cacheManager = require('cache-manager');
const TelegramBot = require('node-telegram-bot-api');
const token = '6580099025:AAHPCIXg6ml5waXlm6lgutS5ZsklHh0Mf98';
const chatId = -749894895;
const bot = new TelegramBot(token, {polling: true});
const help = 'Commands:\n`help` - get help;\n`stat` - get current statistics;\n`update` - update statistics.';
var stat = {
totalHits: 0,
totalMisses: 0,
hits: 0,
misses: 0,
keys: 0,
size: 0,
totalStart: Date.now(),
start: Date.now()
};
var cache;
function updateStat() {
if( cache ) {
cache.keys().then( keys => {
stat.keys = keys.length;
stat.size = 0;
keys.forEach((key) => {
cache.get(key, function (err, content) {
if (!err && content) {
stat.size += Buffer.from(content).length;
}
});
});
});
console.log(`Stat updated.`);
}
}
function sendStat() {
console.log(`Statistics: ${JSON.stringify(stat)}`);
bot.sendMessage(chatId, getHumanStatistics());
stat.hits = 0;
stat.misses = 0;
stat.start = Date.now();
}
function getHumanStatistics() {
return `Cache: ${stat.keys} keys of ${ Math.round(stat.size / 1024 / 1024 * 10) / 10 } Mb.
Total (started at ${new Date(stat.totalStart).toLocaleString('ru-ru')})
- hits : ${stat.totalHits}
- misses: ${stat.totalMisses}
Last (started at ${new Date(stat.start).toLocaleString('ru-ru')})
- hits : ${stat.hits}
- misses: ${stat.misses}`;
}
bot.on('message', (msg) => {
console.log(msg);
if( msg.text == `update` ) {
updateStat();
bot.sendMessage(msg.chat.id, `Statistics is being updated.`)
} else if( msg.text == `stat` ) {
bot.sendMessage(msg.chat.id, getHumanStatistics())
} else if ( msg.text == `help`) {
bot.sendMessage(msg.chat.id, help)
} else {
bot.sendMessage(msg.chat.id, `Command is not recognized.\n\nMessage: ${JSON.stringify(msg)}\n\n${help}`)
}
});
module.exports = {
init: function() {
this.cache = cacheManager.caching({
store: 'memory', max: process.env.CACHE_MAXSIZE || 1000, ttl: process.env.CACHE_TTL || 60 * 60 * 24 * 10 /*seconds*/
});
cache = this.cache;
setInterval(updateStat, (60 * 10 * 1) * 1000); // each 10 min
setInterval(sendStat , (60 * 60 * 24) * 1000); // once a day
bot.sendMessage(chatId, `Prerender started!\n\n${help}`);
},
requestReceived: function(req, res, next) {
this.cache.get(req.prerender.url, function (err, result) {
if (!err && result) {
req.prerender.cacheHit = true;
stat.hits ++;
stat.totalHits ++;
res.send(200, result);
} else {
stat.misses ++;
stat.totalMisses ++;
next();
}
});
},
beforeSend: function(req, res, next) {
if (!req.prerender.cacheHit && req.prerender.statusCode == 200) {
this.cache.set(req.prerender.url, req.prerender.content);
}
next();
},
};