|
1 | | -const RedisPool = require("./redis_pool"); |
2 | | -const bind = require("./util/bind"); |
| 1 | +const RedisPool = require("simple-redis-pool"); |
3 | 2 |
|
4 | 3 | const debug = require("debug")("simpleRedisStore"); |
5 | 4 |
|
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); |
11 | 28 | return result; |
12 | 29 | }) |
13 | 30 | .catch(err => { |
14 | | - pool.release(conn); |
| 31 | + this.pool.release(conn); |
15 | 32 | throw err; |
16 | 33 | })); |
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 | +}; |
27 | 35 |
|
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 | +}; |
30 | 39 |
|
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) { |
34 | 41 |
|
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 | +}; |
46 | 48 |
|
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 | +}; |
51 | 52 |
|
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 | +}; |
54 | 56 |
|
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 | +}; |
60 | 60 |
|
61 | | - return wrapFunc(pool, (conn) => conn.keysAsync(pattern)) |
62 | | - .then(keys => { |
| 61 | +RedisStore.prototype.keys = function (pattern) { |
| 62 | + if (!pattern || pattern === "") { |
| 63 | + pattern = "*"; |
| 64 | + } |
63 | 65 |
|
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 | +}; |
74 | 68 |
|
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 | +}; |
0 commit comments