Skip to content

Commit 411fc17

Browse files
committed
more refactoring
1 parent c2e08bd commit 411fc17

File tree

4 files changed

+137
-40
lines changed

4 files changed

+137
-40
lines changed

lib/redis_store.js

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const RedisPool = require("simple-redis-pool");
2+
const pick = require("lodash.pick");
23

34
const debug = require("debug")("simpleRedisStore");
45

5-
66
const RedisStore = module.exports = function (options) {
77

8+
options = pick(options, ["name", "redisOptions", "poolOptions", "logger"]);
9+
810
this.name = options.name || `redisStore-${Math.random().toString(36).substr(2, 10)}`;
911
this.redisOptions = options.redisOptions;
10-
this.poolOptions = Object.assign({
11-
acquireTimeoutMillis: 200
12-
}, options.poolOptions || {});
12+
this.poolOptions = options.poolOptions;
1313
this.logger = require("./logger")(options.logger);
1414

1515
this.pool = null;
@@ -21,25 +21,40 @@ const RedisStore = module.exports = function (options) {
2121
logger: this.logger
2222
});
2323

24-
this.ping()
25-
.then(resp => {
26-
if (resp === "PONG") {
27-
debug("Redis store created.", this.pool.status());
28-
} else {
29-
debug("expected PONG but got", resp);
30-
const err = new Error("UNKNOWN_PING_RESPONSE");
31-
err.message = "expected PONG but got : " + resp;
32-
throw err;
33-
}
34-
});
35-
24+
// // since pool factory events are not triggered due to retry issue; a workaround
25+
// this.testConnection()
26+
// .then((res) => {
27+
// console.log("#########################", res)
28+
// debug("Redis store created.", this.pool.status())
29+
// });
30+
// this.pool.acquire()
3631
} catch (e) {
3732
debug("Failed to create", e);
3833
this.pool = null;
3934
throw e;
4035
}
4136
};
4237

38+
RedisStore.prototype.testConnection = function () {
39+
40+
debug("PING to test connection");
41+
42+
return this.ping()
43+
.then(resp => {
44+
console.log("resp", resp)
45+
if (resp !== "PONG") {
46+
debug("expected PONG but got", resp);
47+
const err = new Error("UNKNOWN_PING_RESPONSE");
48+
err.message = "expected PONG but got : " + resp;
49+
throw err;
50+
}
51+
})
52+
.catch(e => {
53+
debug("Failed to PING", e);
54+
this.logger.error("Test connection failed", e);
55+
throw e;
56+
});
57+
};
4358

4459
RedisStore.prototype.getName = function () {
4560
return this.pool.getName();
@@ -53,42 +68,45 @@ RedisStore.prototype.getPoolOptions = function () {
5368
return this.pool.getPoolOptions();
5469
};
5570

56-
// verify if the connection is successful or not
71+
RedisStore.prototype.sendCommand = function () {
72+
return this.pool.sendCommand.apply(this, arguments);
73+
};
74+
5775
RedisStore.prototype.ping = function () {
58-
return this.pool.sendCommand("ping");
76+
return this.sendCommand("ping");
5977
};
6078

6179
RedisStore.prototype.get = function (key) {
62-
return this.pool.sendCommand("get", key);
80+
return this.sendCommand("get", key);
6381
};
6482

6583
RedisStore.prototype.set = function (key, value, ttlInSeconds) {
6684

6785
if (ttlInSeconds) {
68-
return this.pool.sendCommand("setex", [key, ttlInSeconds, value]);
86+
return this.sendCommand("setex", [key, ttlInSeconds, value]);
6987
} else {
70-
return this.pool.sendCommand("set", [key, value]);
88+
return this.sendCommand("set", [key, value]);
7189
}
7290
};
7391

7492
RedisStore.prototype.del = function (keys) {
75-
return this.pool.sendCommand("del", keys);
93+
return this.sendCommand("del", keys);
7694
};
7795

7896
RedisStore.prototype.expire = function (key, ttlInSeconds) {
79-
return this.pool.sendCommand("expire", [key, ttlInSeconds]);
97+
return this.sendCommand("expire", [key, ttlInSeconds]);
8098
};
8199

82100
RedisStore.prototype.ttlInSeconds = function (key) {
83-
return this.pool.sendCommand("ttl", key);
101+
return this.sendCommand("ttl", key);
84102
};
85103

86104
RedisStore.prototype.keys = function (pattern) {
87105
if (!pattern || pattern === "") {
88106
pattern = "*";
89107
}
90108

91-
return this.pool.sendCommand("keys", pattern);
109+
return this.sendCommand("keys", pattern);
92110
};
93111

94112
RedisStore.prototype.deleteAll = function (pattern) {
@@ -109,3 +127,7 @@ RedisStore.prototype.deleteAll = function (pattern) {
109127
}
110128
});
111129
};
130+
131+
RedisStore.prototype.status = function () {
132+
return this.pool.status();
133+
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
},
3535
"dependencies": {
3636
"debug": "^2.3.3",
37-
"simple-redis-pool": "^2.0.0"
37+
"lodash.pick": "^4.4.0",
38+
"simple-redis-pool": "^2.0.2"
3839
},
3940
"devDependencies": {
4041
"bluebird": "^3.4.6",
4142
"coveralls": "^2.11.15",
42-
"eslint": "^3.11.0",
43+
"eslint": "^3.11.1",
4344
"istanbul": "^0.4.5",
4445
"mocha": "^3.2.0",
4546
"mocha-lcov-reporter": "^1.2.0",

test/redis_store.js

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,57 @@ const RedisStore = require("../lib/redis_store");
55
describe("redisStore", () => {
66

77
const name = "testStore";
8-
9-
const store = new RedisStore({
10-
name: name,
11-
redisOptions: {
12-
host: process.env.REDIS_HOST || "127.0.0.1"
13-
}});
8+
const redisOptions = {
9+
host: process.env.REDIS_HOST || "127.0.0.1"
10+
};
11+
12+
// describe("constructor", () => {
13+
//
14+
// it("should invalid host fail acquire connection", () => {
15+
//
16+
// (new RedisStore({
17+
// redisOptions: {
18+
// host: "UNAVAILABLE_HOST"
19+
// }
20+
// })).should.throw();
21+
// });
22+
//
23+
// it("should conn timeout fail acquire connection", () => {
24+
//
25+
// (new RedisStore({
26+
// redisOptions: redisOptions,
27+
// poolOptions: {
28+
// acquireTimeoutMillis: 1
29+
// }
30+
// })).should.throw();
31+
// });
32+
// });
1433

1534
describe("getName", () => {
16-
it("should return with given name", () => {
35+
36+
it("should set given name", () => {
37+
38+
const store = new RedisStore({
39+
name: name
40+
});
1741
store.getName().should.be.equal(name);
1842
});
43+
44+
it("should set random name if not set", () => {
45+
46+
const store = new RedisStore();
47+
48+
store.getName().should.not.be.empty();
49+
});
1950
});
2051

2152
describe("get", () => {
53+
54+
const store = new RedisStore({
55+
name: name,
56+
redisOptions: redisOptions
57+
});
58+
2259
it("should retrieve an existing key", () => {
2360

2461
const key = "chuck-norris";
@@ -40,6 +77,12 @@ describe("redisStore", () => {
4077
});
4178

4279
describe("set", () => {
80+
81+
const store = new RedisStore({
82+
name: name,
83+
redisOptions: redisOptions
84+
});
85+
4386
it("should store a value", () => {
4487

4588
const key = "key";
@@ -74,6 +117,12 @@ describe("redisStore", () => {
74117
});
75118

76119
describe("del", () => {
120+
121+
const store = new RedisStore({
122+
name: name,
123+
redisOptions: redisOptions
124+
});
125+
77126
it("should delete an existing key", () => {
78127

79128
const key = "key";
@@ -98,6 +147,12 @@ describe("redisStore", () => {
98147
});
99148

100149
describe("expire", () => {
150+
151+
const store = new RedisStore({
152+
name: name,
153+
redisOptions: redisOptions
154+
});
155+
101156
it("should set a key with expire in seconds", () => {
102157

103158
const key = "key";
@@ -124,6 +179,11 @@ describe("redisStore", () => {
124179

125180
describe("ttl", () => {
126181

182+
const store = new RedisStore({
183+
name: name,
184+
redisOptions: redisOptions
185+
});
186+
127187
before(() => store.deleteAll());
128188

129189
it("should return ttl left for a key in seconds", () => {
@@ -149,6 +209,11 @@ describe("redisStore", () => {
149209

150210
describe("keys", () => {
151211

212+
const store = new RedisStore({
213+
name: name,
214+
redisOptions: redisOptions
215+
});
216+
152217
const keyValues = {key1: "value1", key2: "value2"};
153218

154219
before(() => store.deleteAll());
@@ -171,6 +236,11 @@ describe("redisStore", () => {
171236

172237
describe("deleteAll", () => {
173238

239+
const store = new RedisStore({
240+
name: name,
241+
redisOptions: redisOptions
242+
});
243+
174244
const keyValues = {key1: "value1", key2: "value2"};
175245

176246
beforeEach(() => Promise.all(Object.keys(keyValues)

yarn.lock

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ escope@^3.6.0:
417417
esrecurse "^4.1.0"
418418
estraverse "^4.1.1"
419419

420-
eslint@^3.11.0:
421-
version "3.11.0"
422-
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.11.0.tgz#41d34f8b3e69949beee5c097ff4e75ad13ba2d00"
420+
eslint@^3.11.1:
421+
version "3.11.1"
422+
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.11.1.tgz#408be581041385cba947cd8d1cd2227782b55dbf"
423423
dependencies:
424424
babel-code-frame "^6.16.0"
425425
chalk "^1.1.3"
@@ -936,6 +936,10 @@ lodash.keys@^3.0.0:
936936
lodash.isarguments "^3.0.0"
937937
lodash.isarray "^3.0.0"
938938

939+
lodash.pick:
940+
version "4.4.0"
941+
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
942+
939943
lodash@^4.0.0, lodash@^4.3.0:
940944
version "4.17.2"
941945
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.2.tgz#34a3055babe04ce42467b607d700072c7ff6bf42"
@@ -1298,9 +1302,9 @@ should@^11.1.1:
12981302
should-type-adaptors "^1.0.1"
12991303
should-util "^1.0.0"
13001304

1301-
simple-redis-pool@^2.0.0:
1302-
version "2.0.0"
1303-
resolved "https://registry.yarnpkg.com/simple-redis-pool/-/simple-redis-pool-2.0.0.tgz#46fdcf5b3960a68a16098e67b0a82f52cb6aa70d"
1305+
simple-redis-pool@^2.0.2:
1306+
version "2.0.2"
1307+
resolved "https://registry.yarnpkg.com/simple-redis-pool/-/simple-redis-pool-2.0.2.tgz#376088de3bc1005dcee8327a398673b461288469"
13041308
dependencies:
13051309
bluebird "^3.4.6"
13061310
debug "^2.3.3"

0 commit comments

Comments
 (0)