Skip to content

Commit

Permalink
Merge pull request #146 from shoplineapp/feature/DC-2771-Enhance-hand…
Browse files Browse the repository at this point in the history
…le-connection-to-Redis

Add timeout promise to redis operations
  • Loading branch information
maryShopline1 authored Aug 13, 2024
2 parents 428e6dd + cd2bb2d commit 5fb181e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 50 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ REDIS_PASS
REDIS_HOST
REDIS_PORT
REDIS_DATABASE
REDIS_TIMEOUT_MS
```

**_Please config your config/app.js_**
Expand Down
1 change: 1 addition & 0 deletions example/usingRedis/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DATABASE=nodejs-framework-redis-example
REDIS_TIMEOUT_MS=4000
4 changes: 2 additions & 2 deletions lib/config/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = {
port: process.env.REDIS_PORT,
database: process.env.REDIS_DATABASE,
user: process.env.REDIS_USER,
pass: process.env.REDIS_PASS

pass: process.env.REDIS_PASS,
timeoutMs: process.env.REDIS_TIMEOUT_MS,
}
95 changes: 47 additions & 48 deletions lib/plugins/redis/lib/Redis.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Promise = require('bluebird');

let _sharedRedis = null
let _redisLib

Expand All @@ -9,8 +11,42 @@ class Redis {
init(config) {
this.redis = require('redis')

Promise.promisifyAll(this.redis.RedisClient.prototype)
Promise.promisifyAll(this.redis.Multi.prototype)
const customPromisifier = (originalMethod) => {
return function (...args) {
return new Promise((resolve, reject) => {
let timeout;
// for backward compatibility, only provide timeout mechanism if config.timeoutMs is set
if (config.timeoutMs) {
timeout = setTimeout(() => {
reject(
new Error(`Redis operation "${originalMethod.name}" timed out`)
);
}, config.timeoutMs);
}

const callback = (err, result) => {
if (timeout) {
clearTimeout(timeout);
}
if (err) {
return reject(err);
}
resolve(result);
};

args.push(callback);

originalMethod.apply(this, args);
});
};
};

Promise.promisifyAll(this.redis.RedisClient.prototype, {
promisifier: customPromisifier,
});
Promise.promisifyAll(this.redis.Multi.prototype, {
promisifier: customPromisifier,
});

this.host = config.host
this.port = config.port
Expand Down Expand Up @@ -46,69 +82,32 @@ class Redis {
}

get(id) {
return this.client.getAsync(id);
}

return new Promise((resolve, reject) => {

this.client.get(id, function(err, reply) {

if (err) return reject(err)

return resolve(reply)

})

})

mget(ids) {
return this.client.mgetAsync(ids);
}

set(id, data, option, duration) {

return new Promise((resolve, reject) => {

this.client.set(id, data, option, duration, function(err, reply) {

if (err) return reject(err)

return resolve(reply)

})

})

return this.client.setAsync(id, data, option, duration);
}

del(id) {

return new Promise((resolve, reject) => {

this.client.del(id, function(err, reply) {

if (err) return reject(err)

return resolve(reply)

})

})

return this.client.delAsync(id);
}

disconnect() {

disconnect() {
if (!this.client) return this

this.client.quit()

return this

}

flushdb() {

return this.client.flushdb()

return this.client.flushdbAsync();
}

}

module.exports = Redis
module.exports = Redis

0 comments on commit 5fb181e

Please sign in to comment.