Skip to content

Commit eeced51

Browse files
committed
changes to constructor
1 parent 6d8ab12 commit eeced51

File tree

6 files changed

+197
-101
lines changed

6 files changed

+197
-101
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[![npm version](http://img.shields.io/npm/v/simple-redis-store.svg)](https://npmjs.org/package/simple-redis-store)
1+
[![npm version](http://img.shields.io/npm/v/simple-redis-store.svg?style=flat-square)](https://npmjs.org/package/simple-redis-store)
22
[![Build Status](https://travis-ci.org/pasupulaphani/simple-redis-store.svg?branch=master)](https://travis-ci.org/pasupulaphani/simple-redis-store)
33
[![Coverage Status](https://coveralls.io/repos/github/pasupulaphani/simple-redis-store/badge.svg?branch=master)](https://coveralls.io/github/pasupulaphani/simple-redis-store?branch=master)
4-
[![dependencies Status](https://david-dm.org/pasupulaphani/simple-redis-store/status.svg)](https://david-dm.org/pasupulaphani/simple-redis-store)
5-
[![Gratipay donate button](https://img.shields.io/badge/gratipay-donate-yellow.svg)](https://gratipay.com/simple-redis-store/)
4+
[![Dependency Status](https://www.versioneye.com/user/projects/583c5221d2fd570034b96f95/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/583c5221d2fd570034b96f95)
5+
[![Gratipay donate button](https://img.shields.io/badge/gratipay-donate-yellow.svg?style=flat-square)](https://gratipay.com/simple-redis-store/)
66

77
# simple-redis-store
88
Redis store ready to scale with node-pool support
@@ -11,7 +11,7 @@ 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 constious es6 components such as Map, Set, Promise etc.
14+
This module requires nodejs v4 or above as it has dependencies on es6 components such as Map, Set, Promise etc.
1515

1616
### Getting started
1717

@@ -23,6 +23,20 @@ This module requires nodejs v4 or above as it has dependencies on constious es6
2323
// set
2424
store.set("key", "value");
2525

26+
#### API
27+
28+
- RedisStore([options])
29+
30+
#### `options` object properties
31+
32+
| Property | Default | Description |
33+
|-----------|-----------|-------------|
34+
| name | Random unique string | Name your pool |
35+
| redisOptions | ```{url: redis://127.0.0.1:6379}``` | opts from https://github.com/NodeRedis/node_redis#options-object-properties |
36+
| poolOptions | null | opts from https://github.com/coopernurse/node-pool#createpool |
37+
| logger | null | Inject your custom logger |
38+
39+
2640
### Run tests
2741

2842
bash test.sh

lib/logger.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = function (logger) {
2+
if (!logger) {
3+
logger = {};
4+
}
5+
6+
return Object.assign({
7+
log: function () {},
8+
info: function () {},
9+
warn: function () {},
10+
error: function () {}
11+
}, logger);
12+
};

lib/redis_store.js

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,93 @@ const RedisPool = require("simple-redis-pool");
22

33
const debug = require("debug")("simpleRedisStore");
44

5-
var RedisStore = module.exports = function (name, redisOptions, poolOptions) {
6-
7-
// set default pool options
8-
poolOptions = Object.assign({
9-
acquireTimeoutMillis: 50
10-
}, poolOptions || {});
11-
12-
this.name = name;
13-
this.redisOptions = redisOptions;
14-
this.poolOptions = poolOptions;
15-
this.pool = new RedisPool(name, redisOptions, poolOptions);
16-
debug("Redis store created.", this.pool.status());
17-
18-
this.getName = this.pool.getName;
19-
this.getRedisDB = this.pool.getRedisDB;
20-
this.getPoolStatus = this.pool.status;
21-
};
225

23-
RedisStore.prototype.executeCmd = function (cmd) {
24-
return this.pool.acquire()
25-
.then(conn => cmd(conn)
26-
.then(result => {
27-
this.pool.release(conn);
28-
return result;
29-
})
30-
.catch(err => {
31-
this.pool.release(conn);
6+
const RedisStore = module.exports = function (options) {
7+
8+
this.name = options.name || `redisStore-${Math.random().toString(36).substr(2, 10)}`;
9+
this.redisOptions = options.redisOptions;
10+
this.poolOptions = Object.assign({
11+
acquireTimeoutMillis: 200
12+
}, options.poolOptions || {});
13+
this.logger = require("./logger")(options.logger);
14+
15+
this.pool = null;
16+
try {
17+
this.pool = new RedisPool({
18+
name: this.name,
19+
redisOptions: this.redisOptions,
20+
poolOptions: this.poolOptions,
21+
logger: this.logger
22+
});
23+
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;
3232
throw err;
33-
}));
33+
}
34+
});
35+
36+
} catch (e) {
37+
debug("Failed to create", e);
38+
this.pool = null;
39+
throw e;
40+
}
41+
};
42+
43+
44+
RedisStore.prototype.getName = function () {
45+
return this.pool.getName();
46+
};
47+
48+
RedisStore.prototype.getRedisOptions = function () {
49+
return this.pool.getRedisOptions();
50+
};
51+
52+
RedisStore.prototype.getPoolOptions = function () {
53+
return this.pool.getPoolOptions();
54+
};
55+
56+
// verify if the connection is successful or not
57+
RedisStore.prototype.ping = function () {
58+
return this.pool.sendCommand("ping");
3459
};
3560

3661
RedisStore.prototype.get = function (key) {
37-
return this.executeCmd(conn => conn.getAsync(key));
62+
return this.pool.sendCommand("get", key);
3863
};
3964

4065
RedisStore.prototype.set = function (key, value, ttlInSeconds) {
4166

4267
if (ttlInSeconds) {
43-
return this.executeCmd(conn => conn.setexAsync(key, ttlInSeconds, value));
68+
return this.pool.sendCommand("setex", [key, ttlInSeconds, value]);
4469
} else {
45-
return this.executeCmd(conn => conn.setAsync(key, value));
70+
return this.pool.sendCommand("set", [key, value]);
4671
}
4772
};
4873

4974
RedisStore.prototype.del = function (keys) {
50-
return this.executeCmd(conn => conn.delAsync(keys));
75+
return this.pool.sendCommand("del", keys);
5176
};
5277

5378
RedisStore.prototype.expire = function (key, ttlInSeconds) {
54-
return this.executeCmd(conn => conn.expireAsync(key, ttlInSeconds));
79+
return this.pool.sendCommand("expire", [key, ttlInSeconds]);
5580
};
5681

5782
RedisStore.prototype.ttlInSeconds = function (key) {
58-
return this.executeCmd(conn => conn.ttlAsync(key));
83+
return this.pool.sendCommand("ttl", key);
5984
};
6085

6186
RedisStore.prototype.keys = function (pattern) {
6287
if (!pattern || pattern === "") {
6388
pattern = "*";
6489
}
6590

66-
return this.executeCmd(conn => conn.keysAsync(pattern));
91+
return this.pool.sendCommand("keys", pattern);
6792
};
6893

6994
RedisStore.prototype.deleteAll = function (pattern) {

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
3434
},
3535
"dependencies": {
36-
"debug": "^2.2.0",
37-
"simple-redis-pool": "^1.0.0"
36+
"debug": "^2.3.3",
37+
"simple-redis-pool": "^2.0.0"
3838
},
3939
"devDependencies": {
4040
"bluebird": "^3.4.6",
41-
"coveralls": "^2.11.14",
42-
"eslint": "^3.9.1",
41+
"coveralls": "^2.11.15",
42+
"eslint": "^3.11.0",
4343
"istanbul": "^0.4.5",
44-
"mocha": "^3.1.0",
44+
"mocha": "^3.2.0",
4545
"mocha-lcov-reporter": "^1.2.0",
46-
"should": "^11.1.0"
46+
"should": "^11.1.1"
4747
}
4848
}

test/redis_store.js

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

77
const name = "testStore";
8-
const redisOptions = Object.assign({
9-
host: process.env.REDIS_HOST || "127.0.0.1"
10-
});
118

12-
const store = new RedisStore(name, redisOptions);
9+
const store = new RedisStore({
10+
name: name,
11+
redisOptions: {
12+
host: process.env.REDIS_HOST || "127.0.0.1"
13+
}});
1314

1415
describe("getName", () => {
1516
it("should return with given name", () => {

0 commit comments

Comments
 (0)