forked from OptimalBits/node_acl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-backend.js
182 lines (149 loc) · 3.72 KB
/
redis-backend.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
Redis Backend.
Implementation of the storage backend using Redis
*/
"use strict";
var contract = require('./contract');
var _ = require('lodash');
function noop(){};
function RedisBackend(redis, prefix){
this.redis = redis;
this.prefix = prefix || 'acl';
}
RedisBackend.prototype = {
/**
Begins a transaction
*/
begin : function(){
return this.redis.multi();
},
/**
Ends a transaction (and executes it)
*/
end : function(transaction, cb){
contract(arguments).params('object', 'function').end();
transaction.exec(function(){cb()});
},
/**
Cleans the whole storage.
*/
clean : function(cb){
contract(arguments).params('function').end();
var self = this;
self.redis.keys(self.prefix+'*', function(err, keys){
if(keys.length){
self.redis.del(keys, function(){cb()});
}else{
cb();
}
});
},
/**
Gets the contents at the bucket's key.
*/
get : function(bucket, key, cb){
contract(arguments)
.params('string', 'string|number', 'function')
.end();
key = this.bucketKey(bucket, key);
this.redis.smembers(key, cb);
},
/**
Gets an object mapping each passed bucket to the union of the specified keys inside that bucket.
*/
unions : function(buckets, keys, cb){
contract(arguments)
.params('array', 'array', 'function')
.end();
var redisKeys = {};
var batch = this.redis.batch();
var self = this;
buckets.forEach(function(bucket) {
redisKeys[bucket] = self.bucketKey(bucket, keys);
batch.sunion(redisKeys[bucket], noop);
});
batch.exec(function(err, replies) {
if (!Array.isArray(replies)) {
return {};
}
var result = {};
replies.forEach(function(reply, index) {
if (reply instanceof Error) {
throw reply;
}
result[buckets[index]] = reply;
});
cb(err, result);
});
},
/**
Returns the union of the values in the given keys.
*/
union : function(bucket, keys, cb){
contract(arguments)
.params('string', 'array', 'function')
.end();
keys = this.bucketKey(bucket, keys);
this.redis.sunion(keys, cb);
},
/**
Adds values to a given key inside a bucket.
*/
add : function(transaction, bucket, key, values){
contract(arguments)
.params('object', 'string', 'string|number','string|array|number')
.end();
key = this.bucketKey(bucket, key);
if (Array.isArray(values)){
values.forEach(function(value){
transaction.sadd(key, value);
});
}else{
transaction.sadd(key, values);
}
},
/**
Delete the given key(s) at the bucket
*/
del : function(transaction, bucket, keys){
contract(arguments)
.params('object', 'string', 'string|array')
.end();
var self = this;
keys = Array.isArray(keys) ? keys : [keys]
keys = keys.map(function(key){
return self.bucketKey(bucket, key);
});
transaction.del(keys);
},
/**
Removes values from a given key inside a bucket.
*/
remove : function(transaction, bucket, key, values){
contract(arguments)
.params('object', 'string', 'string|number','string|array|number')
.end();
key = this.bucketKey(bucket, key);
if (Array.isArray(values)){
values.forEach(function(value){
transaction.srem(key, value);
});
}else{
transaction.srem(key, values);
}
},
//
// Private methods
//
bucketKey : function(bucket, keys){
var self = this;
if(Array.isArray(keys)){
return keys.map(function(key){
return self.prefix+'_'+bucket+'@'+key;
});
}else{
return self.prefix+'_'+bucket+'@'+keys;
}
}
}
exports = module.exports = RedisBackend;