-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprerenderRedisCache.js
119 lines (100 loc) · 3.27 KB
/
prerenderRedisCache.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { createClient } = require('redis')
/**
* Basic Config Variables
* redis_url (string) - Redis hostname (defaults to localhost)
* ttl (int) - TTL on keys set in redis (defaults to 1 day)
*/
var REDIS_URL = process.env.REDISTOGO_URL ||
process.env.REDISCLOUD_URL ||
process.env.REDISGREEN_URL ||
process.env.REDIS_URL ||
'redis://127.0.0.1:6379';
var url = require('url');
var TTL = process.env.PAGE_TTL || 86400;
// Parse out the connection vars from the env string.
var connection = url.parse(REDIS_URL);
console.log(connection.port, connection.hostname)
var client = createClient(connection.port, connection.hostname);
client.connect();
var redisOnline = false;
var STATUS_CODES_TO_CACHE = {
200: true,
203: true,
204: true,
206: true,
300: true,
301: true,
404: true,
405: true,
410: true,
414: true,
501: true
};
// Parse out password from the connection string
if (connection.auth) {
client.auth(connection.auth.split(':')[1]);
}
// Make redis connection
// Select Redis database, parsed from the URL
connection.path = (connection.pathname || '/').slice(1);
connection.database = connection.path.length ? connection.path : '0';
client.select(connection.database);
// Catch all error handler. If redis breaks for any reason it will be reported here.
client.on('error', function (error) {
console.warn('Redis Cache Error: ' + error);
});
client.on('ready', function () {
redisOnline = true;
console.log('Redis Cache Connected');
});
client.on('end', function () {
redisOnline = false;
console.warn(
'Redis Cache Conncetion Closed. Will now bypass redis until it\'s back.'
);
});
module.exports = {
requestReceived: function (req, res, next) {
//
if (req.method !== 'GET' || !redisOnline) {
return next();
}
client.get(req.prerender.url, function (error, result) {
if (!error && result) {
var response = JSON.parse(result);
var headers = response.headers;
var key;
for (key in headers) {
if (headers.hasOwnProperty(key) && !/[^\t\x20-\x7e\x80-\xff]/.test(headers[key])) {
res.setHeader(key, headers[key]);
}
}
res.send(response.statusCode, response.content);
} else {
next();
}
});
},
pageLoaded: function (req, res, next) {
if (!redisOnline || !STATUS_CODES_TO_CACHE[req.prerender.statusCode]) {
return next();
}
var key = req.prerender.url;
var response = {
statusCode: req.prerender.statusCode,
content: req.prerender.content.toString(),
headers: req.prerender.headers
};
client.set(key, JSON.stringify(response), function (error, reply) {
// If library set to cache set an expiry on the key.
if (!error && reply && TTL) {
client.expire(key, TTL, function (error, didSetExpiry) {
if (!error && !didSetExpiry) {
console.warn('Could not set expiry for "' + key + '"');
}
});
}
});
next();
}
};