-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-client.js
58 lines (50 loc) · 1.28 KB
/
redis-client.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
const {promisify} = require('util');
const redis = require('redis');
const client = redis.createClient({
host: 'redis-server',
port: 6379
});
async function _getRequestCache(key) {
const get = promisify(client.get).bind(client);
return get(key).then(function(res) {
return res;
});
}
async function _setExRequestCache(key, val) {
const set = promisify(client.set).bind(client);
return set(key, val, 'EX', 36000).then(function(res) {
return;
});
}
async function _getCrawlRecord(key) {
const get = promisify(client.get).bind(client);
return get(key).then(function(res) {
return res;
});
}
async function _setCrawlRecord(key, val) {
const set = promisify(client.set).bind(client);
return set(key, val).then(function(res) {
return;
});
}
client.on('error', (err) => {
console.log(err);
});
client.on('ready', () => {
// console.log('Flushing all DBs for fresh start (only use for prototype)...');
//client.flushall();
console.log('Redis is ready!');
});
client.on('end', () => {
console.log('Redis says goodbye!');
console.log('Crawler process exiting...')
process.exit();
});
module.exports = {
getRequestCache: _getRequestCache,
setExRequestCache: _setExRequestCache,
getCrawlRecord: _getCrawlRecord,
setCrawlRecord: _setCrawlRecord,
client: client
};