Skip to content

Commit 6d8ab12

Browse files
committed
more tests; modularize dependent pkgs
1 parent 69b73ac commit 6d8ab12

File tree

7 files changed

+190
-389
lines changed

7 files changed

+190
-389
lines changed

lib/redis.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/redis_pool.js

Lines changed: 0 additions & 128 deletions
This file was deleted.

lib/redis_store.js

Lines changed: 69 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,86 @@
1-
const RedisPool = require("./redis_pool");
2-
const bind = require("./util/bind");
1+
const RedisPool = require("simple-redis-pool");
32

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

6-
function wrapFunc (pool, func) {
7-
return pool.acquire()
8-
.then((conn) => func(conn)
9-
.then((result) => {
10-
pool.release(conn);
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+
};
22+
23+
RedisStore.prototype.executeCmd = function (cmd) {
24+
return this.pool.acquire()
25+
.then(conn => cmd(conn)
26+
.then(result => {
27+
this.pool.release(conn);
1128
return result;
1229
})
1330
.catch(err => {
14-
pool.release(conn);
31+
this.pool.release(conn);
1532
throw err;
1633
}));
17-
}
18-
19-
function RedisStore (name, redisOptions, poolOptions) {
20-
21-
if (!poolOptions) {
22-
poolOptions = {};
23-
}
24-
poolOptions = Object.assign({
25-
acquireTimeoutMillis: 50
26-
}, poolOptions);
34+
};
2735

28-
const pool = new RedisPool(name, redisOptions, poolOptions);
29-
debug("Redis store created.", pool.status());
36+
RedisStore.prototype.get = function (key) {
37+
return this.executeCmd(conn => conn.getAsync(key));
38+
};
3039

31-
this.getName = bind(pool.getName, pool);
32-
this.getRedisDB = bind(pool.getDB, pool);
33-
this.getPoolStatus = bind(pool.status, pool);
40+
RedisStore.prototype.set = function (key, value, ttlInSeconds) {
3441

35-
this.get = (key) => wrapFunc(pool, (conn) => conn.getAsync(key));
36-
37-
this.set = (key, value) => wrapFunc(pool, (conn) => conn.setAsync(key, value));
38-
39-
this.setex = (key, value, ttlInSeconds) => wrapFunc(pool, (conn) => conn.setexAsync(key, ttlInSeconds, value));
40-
41-
this.del = (key) => wrapFunc(pool, (conn) => conn.delAsync(key));
42-
43-
this.expire = (key, ttlInSeconds) => wrapFunc(pool, (conn) => conn.expireAsync(key, ttlInSeconds));
44-
45-
this.ttlInSeconds = (key) => wrapFunc(pool, (conn) => conn.ttlAsync(key));
42+
if (ttlInSeconds) {
43+
return this.executeCmd(conn => conn.setexAsync(key, ttlInSeconds, value));
44+
} else {
45+
return this.executeCmd(conn => conn.setAsync(key, value));
46+
}
47+
};
4648

47-
this.keys = (pattern) => {
48-
if (!pattern || pattern === "") {
49-
pattern = "*";
50-
}
49+
RedisStore.prototype.del = function (keys) {
50+
return this.executeCmd(conn => conn.delAsync(keys));
51+
};
5152

52-
return wrapFunc(pool, (conn) => conn.keysAsync(pattern));
53-
};
53+
RedisStore.prototype.expire = function (key, ttlInSeconds) {
54+
return this.executeCmd(conn => conn.expireAsync(key, ttlInSeconds));
55+
};
5456

55-
this.deleteAll = (pattern) => {
56-
if (!pattern || pattern === "") {
57-
pattern = "*";
58-
}
59-
debug("clearing redis keys: ", pattern);
57+
RedisStore.prototype.ttlInSeconds = function (key) {
58+
return this.executeCmd(conn => conn.ttlAsync(key));
59+
};
6060

61-
return wrapFunc(pool, (conn) => conn.keysAsync(pattern))
62-
.then(keys => {
61+
RedisStore.prototype.keys = function (pattern) {
62+
if (!pattern || pattern === "") {
63+
pattern = "*";
64+
}
6365

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-
}
71-
});
72-
};
73-
}
66+
return this.executeCmd(conn => conn.keysAsync(pattern));
67+
};
7468

75-
module.exports = RedisStore;
69+
RedisStore.prototype.deleteAll = function (pattern) {
70+
if (!pattern || pattern === "") {
71+
pattern = "*";
72+
}
73+
debug("clearing redis keys: ", pattern);
74+
75+
return this.keys(pattern)
76+
.then(keys => {
77+
78+
if (keys.length > 0) {
79+
debug("deleting keys ", keys);
80+
return this.del(keys);
81+
} else {
82+
debug("no keys exists with pattern: ", pattern);
83+
return Promise.resolve(true);
84+
}
85+
});
86+
};

lib/util/bind.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@
3333
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
3434
},
3535
"dependencies": {
36-
"bluebird": "^3.4.6",
3736
"debug": "^2.2.0",
38-
"generic-pool": "^3.1.1",
39-
"redis": "^2.6.2"
37+
"simple-redis-pool": "^1.0.0"
4038
},
4139
"devDependencies": {
40+
"bluebird": "^3.4.6",
4241
"coveralls": "^2.11.14",
4342
"eslint": "^3.9.1",
4443
"istanbul": "^0.4.5",

0 commit comments

Comments
 (0)