Skip to content

Commit 6f81cd8

Browse files
committed
Add redis implementation
1 parent 3beaf58 commit 6f81cd8

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

lib/store.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var trie = require('./trie.js');
2-
var exec = require('child_process').exec;
32

43
var NotImplemented = function (name) {
54
return {
@@ -42,3 +41,7 @@ exports.MemoryStore = function () {
4241
exports.ExternalStore = function (command) {
4342
return require("./store/external.js").create(BaseStore, command);
4443
};
44+
45+
exports.RedisStore = function (host, port, db) {
46+
return require("./store/redis.js").create(BaseStore, host, port, db);
47+
};

lib/store/redis.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
var redis = require("redis");
2+
3+
var DEFAULT_HOST = "127.0.0.1";
4+
var DEFAULT_PORT = 6379;
5+
var DEFAULT_DB = 0;
6+
7+
function createClient(host, port, db) {
8+
var client = redis.createClient(
9+
port || DEFAULT_PORT,
10+
host || DEFAULT_HOST
11+
);
12+
13+
if (db || 0 !== DEFAULT_DB) {
14+
client.select(db);
15+
}
16+
17+
return client;
18+
}
19+
20+
exports.create = function (BaseStore, host, port, db) {
21+
var client = createClient(host, port, db);
22+
23+
return Object.create(BaseStore, {
24+
get: {
25+
value: function (path, cb) {
26+
var that = this;
27+
28+
client.hgetall(path, function (err, res) {
29+
that.notify(cb, res || undefined);
30+
});
31+
}
32+
},
33+
getTarget: {
34+
value: function (path, cb) {
35+
var that = this;
36+
37+
this.get(path, function (data) {
38+
if (!data) {
39+
that.notify(cb);
40+
return;
41+
}
42+
43+
that.notify(cb, {
44+
prefix: path,
45+
data: data
46+
});
47+
});
48+
}
49+
},
50+
getAll: {
51+
value: function (cb) {
52+
var that = this;
53+
54+
client.keys("*", function (err, keys) {
55+
if (err || keys.length === 0) {
56+
that.notify(cb, {});
57+
return;
58+
}
59+
60+
var routes = {};
61+
var multi = client.multi();
62+
63+
// queue up a command per key
64+
keys.forEach(function (key) { multi.hgetall(key); });
65+
66+
multi.exec(function (err, values) {
67+
keys.forEach(function (key, index) {
68+
routes[key] = values[index];
69+
});
70+
71+
that.notify(cb, routes);
72+
});
73+
});
74+
}
75+
},
76+
add: {
77+
value: function (path, data, cb) {
78+
var that = this;
79+
80+
client.hmset(path, data, function (err, res) {
81+
that.notify(cb);
82+
});
83+
}
84+
},
85+
update: {
86+
value: function (path, data, cb) {
87+
var that = this;
88+
89+
this.get(path, function (current) {
90+
client.hmset(path, Object.assign(current, data), function (err, res) {
91+
that.notify(cb);
92+
});
93+
});
94+
}
95+
},
96+
remove: {
97+
value: function (path, cb) {
98+
var that = this;
99+
100+
client.del(path, function (err, res) {
101+
that.notify(cb);
102+
});
103+
}
104+
},
105+
hasRoute: {
106+
value: function (path, cb) {
107+
var that = this;
108+
109+
client.exists(path, function (err, res) {
110+
that.notify(cb, res === 1);
111+
});
112+
}
113+
}
114+
});
115+
};

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"devDependencies": {
1919
"jasmine": "^2.5.1",
2020
"jshint": "^2.9.2",
21+
"redis": "~2.6",
2122
"nyc": "^6.4.0",
2223
"request": "~2",
2324
"ws": "^1.1"
@@ -29,6 +30,7 @@
2930
"lib/store.js",
3031
"lib/store/external.js",
3132
"lib/store/memory.js",
33+
"lib/store/redis.js",
3234
"lib/trie.js",
3335
"lib/error/*.html",
3436
"bin/configurable-http-proxy"

test/store_spec.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ var sharedExamples = {
9999
it("merges supplied data with existing data", function (done) {
100100
var store = this.subject;
101101

102-
store.add("/my_route", { "version": 1, "test": "value" }, function () {
103-
store.update("/my_route", { "version": 2 }, function () {
102+
store.add("/my_route", { "version": "1", "test": "value" }, function () {
103+
store.update("/my_route", { "version": "2" }, function () {
104104
store.get("/my_route", function (route) {
105-
expect(route.version).toEqual(2);
105+
expect(route.version).toEqual("2");
106106
expect(route.test).toEqual("value");
107107
done();
108108
});
@@ -173,4 +173,16 @@ describe("store", function () {
173173

174174
sharedExamples.store.apply(this);
175175
});
176+
177+
describe("RedisStore", function () {
178+
beforeEach(function () {
179+
this.subject = storeProvider.RedisStore();
180+
});
181+
182+
afterEach(function (done) {
183+
require("redis").createClient().flushdb(done);
184+
});
185+
186+
sharedExamples.store.apply(this);
187+
});
176188
});

0 commit comments

Comments
 (0)