This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remoteStorage-redisStore.js
81 lines (65 loc) · 1.85 KB
/
remoteStorage-redisStore.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
var crypto = require('crypto');
var redis = require('redis').createClient();
redis.on("error", function (err) {
console.log("Error " + err);
});
//rs = remotestorage
function RedisStore(rs) {
rs.util.extend(this, rs.util.getEventEmitter('change'));
this.getPromise = rs.util.getPromise;
this.asyncEach = rs.util.asyncEach;
this.bind = function(fn) {
return rs.util.bind(fn, this);
};
this.prefix = crypto.randomBytes(6).toString() + '.';
rs.storageAdapter.set(this);
console.log('INFO: found redis storage');
}
RedisStore.prototype = {
get: function(path) {
var promise = this.getPromise();
redis.get(this.path(path), function(err, res) {
if (err) console.log('GET Error:', err);
promise.fulfillLater(res);
});
return promise;
},
set: function(path, node) {
var promise = this.getPromise();
redis.set(this.path(path), node, this.bind(function(err) {
if (err) console.log('SET Error:', err, res);
console.log(res)
this.emit('change', {path: path, oldValue: 'bla'});
promise.fulfillLater();
}));
return promise;
},
remove: function(path) {
var promise = this.getPromise();
redis.del(this.path(path), function(err) {
if (err) console.log('DEL Error:', err);
promise.fulfillLater();
});
return promise;
},
forgetAll: function() {
var promise = this.getPromise();
redis.keys(this.prefix + '*', this.bind(function(err, res) {
if (err) console.log('KEYS Error:', err);
this.asyncEach(res, deleteKey).then(function() {
promise.fulfill();
});
function deleteKey(key) {
redis.del(key, function(err) {
if (err) console.log('DEL Error:', err);
});
}
}));
return promise;
},
//private
path: function(path) {
return this.prefix + path;
}
};
module.exports = RedisStore;