11var message = require ( 'debug' ) ( 'apicache' ) ;
2- var memCache = require ( 'memory-cache' ) ;
32var url = require ( 'url' ) ;
43var _ = require ( 'lodash' ) ;
4+ var MemoryCache = require ( './memory-cache' ) ;
55
66var t = {
7+ ms : 1 ,
78 second : 1000 ,
89 minute : 60000 ,
910 hour : 3600000 ,
@@ -13,24 +14,48 @@ var t = {
1314 year : 3600000 * 24 * 365 ,
1415} ;
1516
17+ var instances = [ ] ;
18+
1619function ApiCache ( ) {
20+ var memCache = new MemoryCache ;
21+
1722 var globalOptions = {
1823 debug : false ,
1924 defaultDuration : 3600000 ,
2025 enabled : true ,
2126 appendKey : [ ] ,
2227 jsonp : false ,
23- redisClient : false
28+ redisClient : false ,
29+ statusCodes : {
30+ include : [ ] ,
31+ exclude : [ ] ,
32+ }
2433 } ;
2534
2635 var instance = this ;
27-
2836 var index = null ;
2937
38+ instances . push ( this ) ;
39+ this . id = instances . length ;
40+
3041 function debug ( msg ) {
3142 return globalOptions . debug ? console . log ( '[apicache]: ' + msg ) : message ( msg )
3243 }
3344
45+ function shouldCacheResponse ( response ) {
46+ var opt = globalOptions
47+ var codes = opt . statusCodes
48+
49+ // console.log('shouldCacheResponse', response)
50+ if ( ! response ) return false
51+
52+ if ( codes . exclude && codes . exclude . length && codes . exclude . indexOf ( response . status ) !== - 1 ) return false
53+ if ( codes . include && codes . include . length && codes . include . indexOf ( response . status ) === - 1 ) return false
54+ // console.log('passed!')
55+
56+ return true
57+ }
58+
3459 this . clear = function ( target ) {
3560 var group = index . groups [ target ] ;
3661
@@ -41,7 +66,7 @@ function ApiCache() {
4166 debug ( 'clearing cached entry for "' + key + '"' ) ;
4267
4368 if ( ! globalOptions . redisClient ) {
44- memCache . del ( key ) ;
69+ memCache . delete ( key ) ;
4570 } else {
4671 globalOptions . redisClient . del ( key ) ;
4772 }
@@ -53,7 +78,7 @@ function ApiCache() {
5378 debug ( 'clearing cached entry for "' + target + '"' ) ;
5479
5580 if ( ! globalOptions . redisClient ) {
56- memCache . del ( target ) ;
81+ memCache . delete ( target ) ;
5782 } else {
5883 globalOptions . redisClient . del ( target ) ;
5984 }
@@ -87,6 +112,9 @@ function ApiCache() {
87112 if ( split . length === 3 ) {
88113 var len = parseFloat ( split [ 1 ] ) ;
89114 var unit = split [ 2 ] . replace ( / s $ / i, '' ) . toLowerCase ( ) ;
115+ if ( unit === 'm' ) {
116+ unit = 'ms'
117+ }
90118
91119 return ( len || 1 ) * ( t [ unit ] || 0 ) ;
92120 }
@@ -150,7 +178,7 @@ function ApiCache() {
150178
151179 if ( ! globalOptions . redisClient ) {
152180
153- debug ( 'returning memCached version of "' + key + '"' ) ;
181+ debug ( 'returning memory-cached version of "' + key + '"' ) ;
154182
155183 res . statusCode = cached . status ;
156184 res . set ( cached . headers ) ;
@@ -206,7 +234,7 @@ function ApiCache() {
206234 responseObj . body = ! _ . isUndefined ( b ) ? b : ( ! _ . isNumber ( a ) ? a : null ) ;
207235
208236 // last bypass attempt
209- if ( ! memCache . get ( key ) && ! req . headers [ 'x-apicache-bypass' ] ) {
237+ if ( shouldCacheResponse ( responseObj ) && ! memCache . get ( key ) && ! req . headers [ 'x-apicache-bypass' ] ) {
210238 if ( req . apicacheGroup ) {
211239 debug ( 'group detected "' + req . apicacheGroup + '"' ) ;
212240
@@ -226,7 +254,7 @@ function ApiCache() {
226254 } ) ;
227255
228256 if ( ! globalOptions . redisClient ) {
229- memCache . put ( key , responseObj , duration ) ;
257+ memCache . add ( key , responseObj , duration ) ;
230258 } else {
231259 globalOptions . redisClient . hset ( key , "responseObj" , JSON . stringify ( responseObj ) ) ;
232260 globalOptions . redisClient . hset ( key , "duration" , duration ) ;
@@ -260,10 +288,22 @@ function ApiCache() {
260288 } ;
261289 } ;
262290
291+ this . newInstance = function ( config ) {
292+ var instance = new ApiCache ( ) ;
293+
294+ if ( config ) {
295+ instance . options ( config )
296+ }
297+
298+ return instance
299+ }
300+
301+ this . clone = function ( ) {
302+ return this . newInstance ( this . options ( ) )
303+ }
304+
263305 // initialize index
264306 this . resetIndex ( ) ;
265-
266- return this ;
267307}
268308
269309module . exports = new ApiCache ( ) ;
0 commit comments