-
Notifications
You must be signed in to change notification settings - Fork 31
/
redisCache.js
40 lines (34 loc) · 1.15 KB
/
redisCache.js
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
var redis = require('redis');
var redisClient = redis.createClient({
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || 6379,
});
module.exports = {
beforePhantomRequest: function (phantom, req, res, next) {
console.log('>> beforePhantomRequest()');
// next();
redisClient.get(req.url, function(cached) {
if (cached) {
console.log('Cached: ' + req.url);
if (typeof cached == 'string') {
res.writeHead(200, {
"Content-Type": "text/html"
});
console.log('>> Sending a cached page');
return res.end(cached);
} else {
console.log('>> Cache is not a string :(');
next();
}
} else {
console.log('>> Cache is empty');
next();
}
});
},
onPhantomPageCreate: function (phantom, req, res, next) {
console.log('>> onPhantomPageCreate()');
redisClient.set(req.url, prerender_res.body);
next();
},
};