This repository was archived by the owner on Oct 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed
Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11import redis from 'redis' ;
2+ import chalk from 'chalk' ;
23
34export 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}
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments