Skip to content

Commit bfc085b

Browse files
committed
refactor modular
1 parent cb36364 commit bfc085b

File tree

4 files changed

+48
-47
lines changed

4 files changed

+48
-47
lines changed

lib/logger.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/redis_pool.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const genericPool = require("generic-pool");
44
const redis = require("./redis");
5-
const logger = require("./logger");
65

76
const debug = require("debug")("simpleRedisPool");
87

@@ -19,14 +18,14 @@ function RedisPool (name, redisOptions, poolOptions) {
1918
const client = redis.createClient(redisOptions);
2019

2120
if (["undefined", null, ""].indexOf(this._redisDb) !== -1) {
22-
logger.info("Selected Redis DB: ", this._redisDb);
21+
debug("Selected Redis DB: ", this._redisDb);
2322
debug("Selected Redis DB: ", this._redisDb);
2423
client.select(this._redisDb);
2524
}
2625

2726
// Handle client connection errors.
2827
client.on("error", err => {
29-
logger.error("Redis pool: ", name, err);
28+
debug("Redis pool: ", name, err);
3029
reject(err);
3130
});
3231

@@ -44,7 +43,7 @@ function RedisPool (name, redisOptions, poolOptions) {
4443
try {
4544
// Flush when closing.
4645
client.end(true);
47-
logger.info("Checking pool info after client destroyed. Available count : %s. Pool size: %s", _pool.availableObjectsCount(), _pool.getPoolSize());
46+
debug("Checking pool info after client destroyed. Available count : %s. Pool size: %s", _pool.availableObjectsCount(), _pool.getPoolSize());
4847
resolve();
4948
} catch (err) {
5049
throw new Error("error", "Error destroying redis client.");

lib/redis_store.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const RedisPool = require("./redis_pool");
22
const bind = require("./util/bind");
3-
const logger = require("./logger");
43

54
const debug = require("debug")("simpleRedisStore");
65

@@ -27,7 +26,7 @@ function RedisStore (name, redisOptions, poolOptions) {
2726
}, poolOptions);
2827

2928
const pool = new RedisPool(name, redisOptions, poolOptions);
30-
logger.info("Redis store created.", pool.status());
29+
debug("Redis store created.", pool.status());
3130

3231
this.getName = bind(pool.getName, pool);
3332
this.getRedisDB = bind(pool.getDB, pool);
@@ -57,21 +56,18 @@ function RedisStore (name, redisOptions, poolOptions) {
5756
if (!pattern || pattern === "") {
5857
pattern = "*";
5958
}
60-
logger.info("clearing redis keys: ", pattern);
61-
62-
return pool.acquire()
63-
.then((conn) => {
64-
return conn.keysAsync(pattern)
65-
.then((keys) => {
66-
if (keys.length > 0) {
67-
debug("deleting keys ", keys);
68-
return conn.delAsync(keys)
69-
.then((result) => {
70-
pool.release(conn);
71-
return result;
72-
});
73-
}
74-
});
59+
debug("clearing redis keys: ", pattern);
60+
61+
return wrapFunc(pool, (conn) => conn.keysAsync(pattern))
62+
.then(keys => {
63+
64+
if (keys.length > 0) {
65+
debug("deleting keys ", keys);
66+
return wrapFunc(pool, (conn) => conn.delAsync(keys));
67+
} else {
68+
debug("no keys exists with pattern: ", pattern);
69+
return Promise.resolve(true);
70+
}
7571
});
7672
};
7773
}

test/redis_store.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,36 +185,32 @@ describe("redisStore", function () {
185185

186186
describe("keys", function () {
187187

188-
beforeEach(function (done) {
188+
const keyValues = {key1: "value1", key2: "value2"};
189+
190+
before(function (done) {
189191
store.deleteAll()
190192
.then(() => done());
191193
});
192194

193-
it("should return all the keys", function (done) {
194-
195-
const keyValues = {key1: "value1", key2: "value2"};
195+
beforeEach(function (done) {
196+
Promise.all(Object.keys(keyValues)
197+
.map(key => store.set(key, keyValues[key]))
198+
).then(() => done());
199+
});
196200

197-
for (var key in keyValues) {
198-
store.set(key, keyValues[key]);
199-
}
201+
it("should return all the keys", function (done) {
200202

201203
store.keys()
202-
.then(function (keys) {
204+
.then(keys => {
203205
keyValues.should.have.keys(keys[0], keys[1]);
204206
done();
205207
});
206208
});
207209

208210
it("should return all the keys matches pattern", function (done) {
209211

210-
const keyValues = {key1: "value1", key2: "value2"};
211-
212-
for (var key in keyValues) {
213-
store.set(key, keyValues[key]);
214-
}
215-
216212
store.keys("key[2]")
217-
.then(function (keys) {
213+
.then(keys => {
218214
keys.should.containEql("key2");
219215
done();
220216
});
@@ -223,22 +219,21 @@ describe("redisStore", function () {
223219

224220
describe("deleteAll", function () {
225221

226-
beforeEach(function () {
222+
beforeEach(function (done) {
227223
const keyValues = {key1: "value1", key2: "value2"};
228224

229-
for (var key in keyValues) {
230-
store.set(key, keyValues[key]);
231-
}
225+
Promise.all(Object.keys(keyValues)
226+
.map(key => store.set(key, keyValues[key]))
227+
).then(() => done());
232228
});
233229

234230
it("should delete all the keys", function (done) {
235231

236232
store.deleteAll()
237233
.then(function (v) {
238234
v.should.be.ok();
239-
});
240-
241-
store.keys()
235+
})
236+
.then(store.keys)
242237
.then(function (keys) {
243238
keys.should.be.empty();
244239
done();
@@ -250,14 +245,26 @@ describe("redisStore", function () {
250245
store.deleteAll("key[2]")
251246
.then(function (v) {
252247
v.should.be.ok();
253-
});
254-
255-
store.keys()
248+
})
249+
.then(store.keys)
256250
.then(function (keys) {
257251
keys.should.be.not.empty();
258252
keys.should.not.containEql("key2");
259253
done();
260254
});
261255
});
256+
257+
it("should not delete when nothing matches", function (done) {
258+
259+
store.deleteAll()
260+
.then(function (v) {
261+
v.should.be.ok();
262+
})
263+
.then(() => store.deleteAll("nonExistingKey"))
264+
.then(function (v) {
265+
v.should.be.ok();
266+
done();
267+
});
268+
});
262269
});
263270
});

0 commit comments

Comments
 (0)