diff --git a/README.md b/README.md index aa3f881..4a12de0 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ let cache5min = cache('5 minute') // continue to use normally // 'cache-control': 'no-cache' // example of header overwrite }, respectCacheControl: false|true // If true, 'Cache-Control: no-cache' in the request header will bypass the cache. + maxKey: number // When using local memory, it is necessary to limit the max memory of nodejs. If you want to limit the number of keys that can be cached for this. (default is not using) } ``` diff --git a/src/apicache.js b/src/apicache.js index 8601ba2..7cfe57f 100644 --- a/src/apicache.js +++ b/src/apicache.js @@ -58,6 +58,7 @@ function ApiCache() { }, trackPerformance: false, respectCacheControl: false, + maxKey: -1, } var middlewareOptions = [] @@ -142,7 +143,8 @@ function ApiCache() { debug('[apicache] error in redis.hset()') } } else { - memCache.add(key, value, duration, expireCallback) + if (globalOptions.maxKey > 0 && memCache.size > globalOptions.maxKey) + memCache.add(key, value, duration, expireCallback) } // add automatic cache clearing from duration, includes max limit on setTimeout