Skip to content

Commit f333ebf

Browse files
committed
more refactoring
1 parent 2f7a5a0 commit f333ebf

File tree

11 files changed

+106
-118
lines changed

11 files changed

+106
-118
lines changed

.eslintrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"extends": "eslint:recommended",
33
"env": {
4-
// I write for browser
54
"browser": false,
6-
// in CommonJS
75
"node": true
86
},
7+
"parserOptions": {
8+
"ecmaVersion": 6
9+
},
910
"globals": {
1011
"describe": false,
1112
"before": false,

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ node_js:
77
services:
88
- redis-server
99
after_success:
10+
- npm run lint
1011
- npm run coveralls

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
22
Version 2, December 2004
33

4-
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
4+
Copyright (C) 2004 Sam Hoceconst <sam@hoceconst.net>
55

66
Everyone is permitted to copy and distribute verbatim or modified
77
copies of this license document, and changing it is allowed as long

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ Redis store ready to scale with node-pool support
1111
1212
## Prerequisites
1313

14-
This module requires nodejs v4 or above as it has dependencies on various es6 components such as Map, Set, Promise etc.
14+
This module requires nodejs v4 or above as it has dependencies on constious es6 components such as Map, Set, Promise etc.
1515

1616
### Getting started
1717

1818
npm install simple-redis-store
1919

20-
var RedisStore = require("simple-redis-store");
21-
var store = new RedisStore();
20+
const RedisStore = require("simple-redis-store");
21+
const store = new RedisStore();
2222

2323
// set
2424
store.set("key", "value");

lib/redis.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var redis = require("redis");
2-
var Bluebird = require("bluebird");
1+
const redis = require("redis");
2+
const Bluebird = require("bluebird");
33

44
Bluebird.promisifyAll(redis.RedisClient.prototype);
55
Bluebird.promisifyAll(redis.Multi.prototype);

lib/redis_pool.js

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,71 @@
1-
var genericPool = require("generic-pool");
2-
var redis = require("./redis");
3-
var logger = require("./logger");
1+
"use strict";
42

5-
var debug = require("debug")("simpleRedisPool");
3+
const genericPool = require("generic-pool");
4+
const redis = require("./redis");
5+
const logger = require("./logger");
66

7-
function RedisPool (name, redisOptions, poolOptions) {
7+
const debug = require("debug")("simpleRedisPool");
88

9-
this._redisDb = redisOptions.db || 0;
10-
11-
var _pool = null;
12-
var _poolOptions = {};
13-
_poolOptions.name = name;
14-
15-
// Build new Redis database clients.
16-
_poolOptions.create = function (cb) {
17-
var client = redis.createClient(redisOptions);
18-
19-
if (["undefined", null, ""].indexOf(this._redisDb) !== -1) {
20-
logger.info("Selected Redis DB: ", this._redisDb);
21-
debug("Selected Redis DB: ", this._redisDb);
22-
client.select(this._redisDb);
23-
}
24-
25-
// Handle client connection errors.
26-
client.on("error", function (err) {
27-
logger.error("Redis pool: ", name, err);
28-
throw err;
29-
});
9+
function RedisPool (name, redisOptions={}, poolOptions={}) {
3010

31-
// Register the authentication password if needed.
32-
if (redisOptions.auth_pass) {
33-
client.auth(redisOptions.auth_pass);
34-
}
35-
36-
cb(null, client);
37-
};
38-
39-
// The destroy function is called when client connection needs to be closed.
40-
_poolOptions.destroy = function (client) {
41-
try {
42-
// Flush when closing.
43-
client.end(true);
44-
logger.info("Checking pool info after client destroyed. Available count : %s. Pool size: %s", _pool.availableObjectsCount(), _pool.getPoolSize());
11+
this._redisDb = redisOptions.db || 0;
4512

46-
} catch (err) {
47-
throw new Error("error", "Error destroying redis client.");
13+
let _pool = null;
14+
15+
const factory = {
16+
create: () => {
17+
return new Promise((resolve, reject) => {
18+
19+
const client = redis.createClient(redisOptions);
20+
21+
if (["undefined", null, ""].indexOf(this._redisDb) !== -1) {
22+
logger.info("Selected Redis DB: ", this._redisDb);
23+
debug("Selected Redis DB: ", this._redisDb);
24+
client.select(this._redisDb);
25+
}
26+
27+
// Handle client connection errors.
28+
client.on("error", err => {
29+
logger.error("Redis pool: ", name, err);
30+
reject(err);
31+
});
32+
33+
// Register the authentication password if needed.
34+
if (redisOptions.auth_pass) {
35+
client.auth(redisOptions.auth_pass);
36+
}
37+
38+
resolve(client);
39+
});
40+
},
41+
destroy: (client) => {
42+
return new Promise((resolve) => {
43+
44+
try {
45+
// Flush when closing.
46+
client.end(true);
47+
logger.info("Checking pool info after client destroyed. Available count : %s. Pool size: %s", _pool.availableObjectsCount(), _pool.getPoolSize());
48+
resolve();
49+
} catch (err) {
50+
throw new Error("error", "Error destroying redis client.");
51+
}
52+
});
4853
}
4954
};
5055

51-
if (poolOptions) {
52-
Object.assign(_poolOptions, poolOptions);
53-
}
54-
5556
// Now that the pool settings are ready create a pool instance.
56-
_pool = new genericPool.Pool(_poolOptions);
57+
_pool = genericPool.createPool(factory, poolOptions);
58+
59+
// set name
60+
this.name = name;
5761

5862
// Acquire a database connection and use an optional priority.
59-
this.acquire = function (cb, priority) {
60-
_pool.acquire(cb, priority);
63+
this.acquire = (priority) => {
64+
return _pool.acquire(priority);
6165
};
6266

63-
this.acquireDb = function (cb, db, priority) {
64-
_pool.acquire(function (err, client) {
67+
this.acquireDb = (cb, db, priority) => {
68+
_pool.acquire((err, client) => {
6569
if (!err && client._db_selected !== db) {
6670
client["_db_selected"] = db;
6771
client.select(db);
@@ -73,7 +77,7 @@ function RedisPool (name, redisOptions, poolOptions) {
7377
};
7478

7579
// Release a database connection to the pool.
76-
this.release = function (client) {
80+
this.release = (client) => {
7781
// Always reset the DB to the default. This prevents issues
7882
// if a user used the select command to change the DB.
7983

@@ -85,8 +89,8 @@ function RedisPool (name, redisOptions, poolOptions) {
8589
};
8690

8791
// Drains the connection pool and call the callback id provided.
88-
this.drain = function (cb) {
89-
_pool.drain(function () {
92+
this.drain = (cb) => {
93+
_pool.drain(() => {
9094
_pool.destroyAllNow();
9195
if (cb) {
9296
cb();
@@ -95,33 +99,33 @@ function RedisPool (name, redisOptions, poolOptions) {
9599
};
96100

97101
// Returns factory.name for this pool
98-
this.getName = function () {
99-
return _pool.getName();
102+
this.getName = () => {
103+
return this.name;
100104
};
101105

102-
this.getDB = function () {
106+
this.getDB = () => {
103107
return this._redisDb;
104108
};
105109

106-
this.getPoolSize = function () {
107-
return _pool.getPoolSize();
110+
this.getPoolSize = () => {
111+
return _pool.size;
108112
};
109113

110-
this.availableObjectsCount = function () {
111-
return _pool.availableObjectsCount();
114+
this.availableObjectsCount = () => {
115+
return _pool.available;
112116
};
113117

114-
this.waitingClientsCount = function () {
115-
return _pool.waitingClientsCount();
118+
this.waitingClientsCount = () => {
119+
return _pool.pending;
116120
};
117121

118-
this.status = function () {
122+
this.status = () => {
119123
return {
120-
name: _pool.getName(),
121-
size: _pool.getPoolSize(),
124+
name: this.name,
125+
size: _pool.size,
122126
db: this._redisDb,
123-
avail: _pool.availableObjectsCount(),
124-
waiting: _pool.waitingClientsCount()
127+
avail: _pool.available,
128+
waiting: _pool.pending
125129
};
126130
};
127131
}

lib/redis_store.js

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
var Bluebird = require("bluebird");
2-
var RedisPool = require("./redis_pool");
3-
var bind = require("./util/bind");
4-
var logger = require("./logger");
1+
const RedisPool = require("./redis_pool");
2+
const bind = require("./util/bind");
3+
const logger = require("./logger");
54

6-
var debug = require("debug")("simpleRedisStore");
5+
const debug = require("debug")("simpleRedisStore");
76

87
function RedisStore (name, redisOptions, poolOptions) {
98

10-
var pool = new RedisPool(name, redisOptions, poolOptions);
9+
const pool = new RedisPool(name, redisOptions, poolOptions);
1110
logger.info("Redis store created.", pool.status());
1211

13-
function acquire () {
14-
return new Bluebird(function (resolve, reject) {
15-
pool.acquire(function (err, conn) {
16-
if (err) {
17-
pool.release(conn);
18-
return reject(err);
19-
}
20-
21-
if (redisOptions.db) {
22-
conn.select(redisOptions.db);
23-
}
24-
25-
resolve(conn);
26-
});
27-
});
28-
}
29-
3012
function release (conn) {
3113
return pool.release(conn);
3214
}
@@ -37,7 +19,7 @@ function RedisStore (name, redisOptions, poolOptions) {
3719

3820
this.get = function (key) {
3921

40-
return acquire()
22+
return pool.acquire()
4123
.then(function (conn) {
4224
return conn.getAsync(key)
4325
.then(function (result) {
@@ -49,7 +31,7 @@ function RedisStore (name, redisOptions, poolOptions) {
4931

5032
this.set = function (key, value) {
5133

52-
return acquire()
34+
return pool.acquire()
5335
.then(function (conn) {
5436
return conn.setAsync(key, value)
5537
.then(function (result) {
@@ -61,7 +43,7 @@ function RedisStore (name, redisOptions, poolOptions) {
6143

6244
this.setex = function (key, value, ttl) {
6345

64-
return acquire()
46+
return pool.acquire()
6547
.then(function (conn) {
6648
return conn.setexAsync(key, ttl, value)
6749
.then(function (result) {
@@ -73,7 +55,7 @@ function RedisStore (name, redisOptions, poolOptions) {
7355

7456
this.del = function (key) {
7557

76-
return acquire()
58+
return pool.acquire()
7759
.then(function (conn) {
7860
return conn.delAsync(key)
7961
.then(function (result) {
@@ -85,7 +67,7 @@ function RedisStore (name, redisOptions, poolOptions) {
8567

8668
this.ttl = function (key) {
8769

88-
return acquire()
70+
return pool.acquire()
8971
.then(function (conn) {
9072
return conn.ttlAsync(key)
9173
.then(function (result) {
@@ -100,7 +82,7 @@ function RedisStore (name, redisOptions, poolOptions) {
10082
pattern = "*";
10183
}
10284

103-
return acquire()
85+
return pool.acquire()
10486
.then(function (conn) {
10587
return conn.keysAsync(pattern)
10688
.then(function (result) {
@@ -117,7 +99,7 @@ function RedisStore (name, redisOptions, poolOptions) {
11799
}
118100
logger.info("clearing redis keys: ", pattern);
119101

120-
return acquire()
102+
return pool.acquire()
121103
.then(function (conn) {
122104
return conn.keysAsync(pattern)
123105
.then(function (keys) {

lib/util/bind.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* @example
55
*
6-
* var obj = {a: 1, b: function() { console.log(a); }};
7-
* var bound = utils.bind(obj, obj.b);
6+
* const obj = {a: 1, b: function() { console.log(a); }};
7+
* const bound = utils.bind(obj, obj.b);
88
* bound(); // prints 1
99
*
1010
* @param {Function} fn The function to bind.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"dependencies": {
3636
"bluebird": "^3.4.6",
3737
"debug": "^2.2.0",
38-
"generic-pool": "^2.4.2",
38+
"generic-pool": "^3.1.1",
3939
"redis": "^2.6.2"
4040
},
4141
"devDependencies": {

test/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
require("should");
2-
var RedisStore = require("../lib/redis_store");
2+
const RedisStore = require("../lib/redis_store");
33

44
describe("redisStore", function () {
55

6-
var redisOptions = Object.assign({
6+
const redisOptions = Object.assign({
77
host: process.env.REDIS_HOST || "127.0.0.1"
88
});
9-
var store = new RedisStore("testStore", redisOptions);
9+
const store = new RedisStore("testStore", redisOptions);
1010

1111
describe("set", function () {
1212
it("set", function (done) {
@@ -21,8 +21,8 @@ describe("redisStore", function () {
2121

2222
describe("get", function () {
2323

24-
var key = "chuck-norris";
25-
var value = "alias: superman";
24+
const key = "chuck-norris";
25+
const value = "alias: superman";
2626

2727
before(function (done) {
2828
store.set(key, value)

0 commit comments

Comments
 (0)