node-cache icon indicating copy to clipboard operation
node-cache copied to clipboard

RangeError: Maximum call stack size exceeded

Open relisiuol opened this issue 5 years ago • 2 comments

Hello,

I tried to use this library to optimize a website speed, that worked great but sometimes a crash occurs. If you can help, that would be nice.

Log:

[22/Feb/2020:10:03:18 +0100] [upstream] Upstream started PID: 1163837
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR: internal/timers.js:279
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR: function incRefCount() {
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:                     ^
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR: 
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR: RangeError: Maximum call stack size exceeded
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at incRefCount (internal/timers.js:279:21)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at insert (internal/timers.js:336:7)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at active (internal/timers.js:292:3)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at setTimeout (timers.js:143:3)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at Cache.put (node_modules/memory-cache/index.js:34:24)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at ServerResponse.res.send [as sendResponse] (app.js:69:16)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at ServerResponse.res.send [as sendResponse] (app.js:70:13)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at ServerResponse.res.send [as sendResponse] (app.js:70:13)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at ServerResponse.res.send [as sendResponse] (app.js:70:13)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] STDERR:     at ServerResponse.res.send [as sendResponse] (app.js:70:13)
[22/Feb/2020:10:15:26 +0100] [upstream] [1163837] Upstream stopped (reason: exited spontaneously with return code 1)

Cache function into app.js

const mcache = require('memory-cache');

var cache = (duration) => {
  return (req, res, next) => {
    let key = '__express__' + req.originalUrl || req.url
    let cachedBody = mcache.get(key)
    if (cachedBody) {
      res.send(cachedBody)
      return
    } else {
      res.sendResponse = res.send
      res.send = (body) => {
        mcache.put(key, body, duration * 1000);
        res.sendResponse(body)
      }
      next()
    }
  }
}

Have a good day'

relisiuol avatar Feb 22 '20 09:02 relisiuol

The problem is not in the library, the error it shows you is an issue in the memory of your server. You are caching or in RAM and add to that the processes that your application is running, until a moment comes when that memory is exhausted and when the garbage collector releases a little of that memory the error disappears. You must be careful because when your application grows or traffic increases, those errors can be more frequent. You should analyze when it is convenient to cache the responses from your server

emauriciomoreno avatar Jun 01 '20 20:06 emauriciomoreno

I can guess it may happen when you cache too many data. Also you should keep it on mind there is a difference between server and your local machine power. For caching a large amount of data you should take a look on redis

IvanAdmaers avatar Jan 22 '22 16:01 IvanAdmaers