-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis-set-storage.js
70 lines (62 loc) · 2.29 KB
/
redis-set-storage.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
function RedisSetStorage(options) {
this.redisClient = options.redisClient;
this.key = options.key;
this.keyPrepend = "js:ls:";
var self = this;
// Middleware for connect.js/express.js
this.middleware = {
// Returns a JSON list of all the items
get : function (req, res) {
self.getItems(function (err, result) {
if (!err) {
res.json( result );
} else {
res.json(500, { error: err });
}
});
},
// Expects an JSON object in the request with an array of items called `items`.
delete : function (req, res) {
// Add items
if (req.body.items instanceof Array) {
self.removeItems(req.body.items, function (err, result) {
if (!err) {
res.json( result );
} else {
res.json(500, { error: err });
}
});
} else {
res.json(500, { error: "No `items` array in request body" });
}
},
// Expects an JSON object in the request with an array of items called `items`.
add : function (req, res) {
// Add items
if (req.body.items instanceof Array) {
self.addItems(req.body.items, function (err, result) {
if (!err) {
res.json( result );
} else {
res.json(500, { error: err });
}
});
} else {
res.json(500, { error: "No `items` array in request body" });
}
}
};
}
RedisSetStorage.prototype.addItems = function (arr, callback) {
this.redisClient.sadd(this.keyPrepend+this.key, arr, callback);
};
RedisSetStorage.prototype.removeItems = function (arr, callback) {
this.redisClient.srem(this.keyPrepend+this.key, arr, callback);
};
RedisSetStorage.prototype.getItems = function (callback) {
this.redisClient.smembers(this.keyPrepend+this.key, callback);
};
RedisSetStorage.prototype.hasItem = function (item, callback) {
this.redisClient.sismember(this.keyPrepend+this.key, item, callback);
};
module.exports = RedisSetStorage;