Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 521f938

Browse files
committed
merge
2 parents 9fc460d + d533687 commit 521f938

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/hooks/redis.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export function before(options) { // eslint-disable-line no-unused-vars
1616

1717
return new Promise(resolve => {
1818
const client = hook.app.get('redisClient');
19+
20+
if (!client) {
21+
resolve(hook);
22+
}
23+
1924
const path = parsePath(hook, options);
2025

2126
client.get(path, (err, reply) => {
@@ -53,6 +58,11 @@ export function after(options) { // eslint-disable-line no-unused-vars
5358
if (!hook.result.cache.cached) {
5459
const duration = hook.result.cache.duration || options.defaultDuration;
5560
const client = hook.app.get('redisClient');
61+
62+
if (!client) {
63+
resolve(hook);
64+
}
65+
5666
const path = hook.params.cacheKey || parsePath(hook, options);
5767

5868
// adding a cache object

src/redisClient.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import redis from 'redis';
2+
import chalk from 'chalk';
23

34
export default function redisClient() { // eslint-disable-line no-unused-vars
4-
this.set('redisClient', redis.createClient(this.get('redis')));
5+
const app = this;
6+
const cacheOptions = app.get('redisCache') || {};
7+
const retryInterval = cacheOptions.retryInterval || 10000;
8+
const redisOptions = Object.assign({}, this.get('redis'), {
9+
retry_strategy: function (options) { // eslint-disable-line camelcase
10+
app.set('redisClient', undefined);
11+
if (cacheOptions.env !== 'test') {
12+
console.log(`${chalk.yellow('[redis]')} not connected`);
13+
}
14+
return retryInterval;
15+
}
16+
});
17+
const client = redis.createClient(redisOptions);
18+
19+
client.on('ready', () => {
20+
app.set('redisClient', client);
21+
if (cacheOptions.env !== 'test') {
22+
console.log(`${chalk.green('[redis]')} connected`);
23+
}
24+
});
525
return this;
626
}

test/client.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from 'chai';
2+
import RedisClient from '../src/redisClient';
3+
4+
const app = {
5+
get: function(key) {
6+
return this[key];
7+
},
8+
set: function(key, val) {
9+
this[key] = val;
10+
},
11+
configure: function(fn) {
12+
fn.call(this);
13+
}
14+
};
15+
16+
describe('redisClient', () => {
17+
it('should not exist if redis not connected', (done) => {
18+
app.set('redis', { port: 1234 }); // force connection error
19+
app.configure(RedisClient);
20+
setTimeout(function () {
21+
expect(app.get('redisClient')).to.not.exist;
22+
done();
23+
}, 500);
24+
});
25+
it('should be available if redis connected', (done) => {
26+
app.set('redis', { port: 6379 }); // restore default
27+
app.configure(RedisClient);
28+
setTimeout(function () {
29+
expect(app.get('redisClient')).to.exist;
30+
done();
31+
}, 500);
32+
});
33+
});

0 commit comments

Comments
 (0)