-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.js
87 lines (61 loc) · 2.06 KB
/
sample.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
// After starting this example load http://localhost:8080 and hit refresh, you will notice that it loads the response from cache for the first 5 seconds and then reloads the cache
// Load modules
var Catbox = require('catbox');
var Http = require('http');
// Declare internals
var internals = {};
internals.handler = function (req, res) {
internals.getResponse(function (err, item) {
if (err) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(item);
}
});
};
internals.getResponse = function (callback) {
var key = {
segment: 'example',
id: 'myExample'
};
var cacheValue = 'my example';
var ttl = 10000; // How long item will be cached in milliseconds
internals.client.get(key, function (err, cached) {
if (err) {
return callback(err);
}
else if (cached) {
return callback(null, 'From cache: ' + cached.item);
}
else {
internals.client.set(key, cacheValue, ttl, function (error) {
callback(error, cacheValue);
});
}
});
};
internals.startCache = function (callback) {
var options = {
partition: 'catbox-examples',
host: process.env.REDIS_PORT_6379_TCP_ADDR || 'localhost',
port: process.env.REDIS_PORT_6379_TCP_PORT || '6379',
password: ''
};
internals.client = new Catbox.Client(require('catbox-redis'), options); // Chance require('../') to 'catbox-redis' when running in your project
internals.client.start(callback);
};
internals.startServer = function (err) {
if (err) {
console.log(err);
console.log('Could not connect to redis. Ending process.')
process.exit();
} else {
var server = Http.createServer(internals.handler);
server.listen(3005);
console.log('Server started at http://localhost:8080/');
}
};
internals.startCache(internals.startServer);