-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisDB.js
90 lines (68 loc) · 1.9 KB
/
redisDB.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
var Q = require('q'),
redis = require('redis'),
uri = 'redis://',
port = '6379',
TABLE_NAME = 'CS550',
RedisClient;
function init(ip, callback) {
var deferred = Q.defer();
var url = uri + ip + ':' + port;
RedisClient = redis.createClient(url);
RedisClient.on('error', function (err) {
console.log("Error : ", err);
process.exit();
});
deferred.resolve(true);
return deferred.promise.nodeify(callback);
}
function createTable(callback) {
var deferred = Q.defer();
deferred.resolve(true);
return deferred.promise.nodeify(callback);
}
// NOTE: Add key-value to table (key : 'key' value: 'value')
function putValue(key, value, callback) {
var deferred = Q.defer();
RedisClient.set(key, value, function (err, result) {
if (err) deferred.reject(false);
else deferred.resolve(true);
});
return deferred.promise.nodeify(callback);
}
// NOTE: Get value from table (key : 'key')
function getValue(key, callback) {
var deferred = Q.defer();
RedisClient.get(key, function (err, result) {
if (err) deferred.reject(false);
else deferred.resolve(true);
});
return deferred.promise.nodeify(callback);
}
// NOTE: Get value from table (key : 'key')
function deleteKey(key, callback) {
var deferred = Q.defer();
RedisClient.del(key, function (err, result) {
if (err) deferred.reject(false);
else deferred.resolve(true);
});
return deferred.promise.nodeify(callback);
}
function getSize(callback) {
var deferred = Q.defer();
RedisClient.keys('*', function (err, result) {
if (err) deferred.reject(false);
else deferred.resolve(result.length);
});
return deferred.promise.nodeify(callback);
}
function tearDown() {
RedisClient.quit();
}
module.exports = {
init: init,
putValue: putValue,
getValue: getValue,
deleteKey: deleteKey,
getSize: getSize,
tearDown: tearDown
};