-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconvert-cache.js
52 lines (48 loc) · 1.32 KB
/
convert-cache.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
/**
* Created by billtt on 3/13/16.
*/
var fs = require('fs');
var config = require('config');
var Util = require('./util');
var redis = require('redis').createClient(config.get('cache.port'), config.get('cache.host'), config.get('cache.options'));
var cache = [];
var current = 0;
function insertNext() {
if (current >= cache.length) {
console.info('Done');
return;
}
var record = cache[current++];
redis.set(record.domain, JSON.stringify(record), function(err) {
if (err) {
console.error(err);
return;
}
insertNext();
});
if (current % 1000 == 0) {
console.info('%d done', current);
}
}
Util.initLog4JS();
redis.on('ready', function() {
console.info('Cache server ready');
var file = process.argv[2];
fs.readFile(file, function(err, data) {
if (err || !data) {
console.info('Cannot load cache file.');
return;
}
try {
var records = JSON.parse(data);
for (var domain in records) {
cache.push(records[domain]);
}
console.info('Cache loaded with %d records.', cache.length);
console.info('Converting...');
insertNext();
} catch (e) {
console.error(e);
}
});
});