-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.js
58 lines (51 loc) · 1.37 KB
/
redis.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
var redis = require('redis');
var settings = require('./settings.js');
client = redis.createClient(settings.redis.port, settings.redis.ip);
var store = function(data, callback) {
client.set(data.id, JSON.stringify(data), function (error) {
if(error) return callback(error);
return callback(null);
});
};
var storeMult = function(data, callback) {
if(typeof(data) !== 'object' && data !== null) {
return callback('Data must be an array.');
}
if(data.length === 0) {
return callback('Must have 1 or more values to store.');
}
for(var dataCounter = 0; storyCounter < data.length; dataCounter++) {
store(data[dataCounter], function (error) {
if(error) return callback(error);
return callback(null);
});
}
};
var read = function(key, callback) {
if(typeof(key) !== 'string') {
key = key.toString();
}
client.get(key, function (error, reply) {
if(error) return callback(error);
return callback(null, JSON.parse(reply));
});
};
var getTotal = function(callback) {
client.keys('*', function (error, keys) {
if(error) return callback(error);
return callback(null, keys.length);
});
};
var backupData = function(callback) {
client.bgsave(function (error, result) {
if(error) return callback(error);
return callback(null, result);
});
};
module.exports = {
store: store,
storeMult: storeMult,
read: read,
getTotal: getTotal,
backupData: backupData
}