@@ -2,17 +2,20 @@ import moment from 'moment';
22import chalk from 'chalk' ;
33import { parsePath } from './helpers/path' ;
44
5- const defaults = { } ;
5+ const defaults = {
6+ env : 'production' ,
7+ defaultDuration : 3600 * 24
8+ } ;
69
710export function before ( options ) { // eslint-disable-line no-unused-vars
8- options = Object . assign ( { } , defaults , options ) ;
9-
1011 return function ( hook ) {
12+ const cacheOptions = hook . app . get ( 'redisCache' ) ;
13+
14+ options = Object . assign ( { } , defaults , cacheOptions , options ) ;
15+
1116 return new Promise ( resolve => {
1217 const client = hook . app . get ( 'redisClient' ) ;
13- const cacheOptions = hook . app . get ( 'redisCache' ) ;
14- const env = cacheOptions . env || 'production' ;
15- const path = parsePath ( hook , cacheOptions ) ;
18+ const path = parsePath ( hook , options ) ;
1619
1720 client . get ( path , ( err , reply ) => {
1821 if ( err !== null ) resolve ( hook ) ;
@@ -24,7 +27,7 @@ export function before(options) { // eslint-disable-line no-unused-vars
2427 resolve ( hook ) ;
2528
2629 /* istanbul ignore next */
27- if ( env !== 'test' ) {
30+ if ( options . env !== 'test' ) {
2831 console . log ( `${ chalk . cyan ( '[redis]' ) } returning cached value for ${ chalk . green ( path ) } .` ) ;
2932 console . log ( `> Expires on ${ duration } .` ) ;
3033 }
@@ -37,17 +40,16 @@ export function before(options) { // eslint-disable-line no-unused-vars
3740} ;
3841
3942export function after ( options ) { // eslint-disable-line no-unused-vars
40- options = Object . assign ( { } , defaults , options ) ;
41-
4243 return function ( hook ) {
44+ const cacheOptions = hook . app . get ( 'redisCache' ) ;
45+
46+ options = Object . assign ( { } , defaults , cacheOptions , options ) ;
47+
4348 return new Promise ( resolve => {
4449 if ( ! hook . result . cache . cached ) {
45- const cacheOptions = hook . app . get ( 'redisCache' ) ;
46- const env = cacheOptions . env || 'production' ;
47- const cachingDefault = cacheOptions . defaultDuration ? cacheOptions . defaultDuration : 3600 * 24 ;
48- const duration = hook . result . cache . duration || cachingDefault ;
50+ const duration = hook . result . cache . duration || options . defaultDuration ;
4951 const client = hook . app . get ( 'redisClient' ) ;
50- const path = parsePath ( hook , cacheOptions ) ;
52+ const path = parsePath ( hook , options ) ;
5153
5254 // adding a cache object
5355 Object . assign ( hook . result . cache , {
@@ -66,7 +68,7 @@ export function after(options) { // eslint-disable-line no-unused-vars
6668 }
6769
6870 /* istanbul ignore next */
69- if ( env !== 'test' ) {
71+ if ( options . env !== 'test' ) {
7072 console . log ( `${ chalk . cyan ( '[redis]' ) } added ${ chalk . green ( path ) } to the cache.` ) ;
7173 console . log ( `> Expires in ${ moment . duration ( duration , 'seconds' ) . humanize ( ) } .` ) ;
7274 }
0 commit comments