Skip to content

CacheableMemory leaks memory and throws errors when hashCache faces the limit #1123

@PaulAnnekov

Description

@PaulAnnekov

Describe the bug

CacheableMemory has an internal hash key cache (it's a Map). Every time you try to set/get some value by key, the key is first checked in hash key and then hashed + added if not found. I couldn't find code which manages the size of the Map. If your key set is not static (and usually, it's not), this Map grows infinitely, consuming more and more memory, until you will face with Map size limit (17M entries). Here is the place where error is thrown at the end:

this._hashCache.set(key, result);

And here is an example of the error:

RangeError: Map maximum size exceeded\n    at Map.set (<anonymous>)\n    at CacheableMemory.hashKey (/home/nodejs/app/node_modules/cacheable/dist/index.cjs:709:21)\n    at CacheableMemory.getStore (/home/nodejs/app/node_modules/cacheable/dist/index.cjs:651:24)\n    at CacheableMemory.get (/home/nodejs/app/node_modules/cacheable/dist/index.cjs:444:24)\n    at Keyv.get (/home/nodejs/app/node_modules/keyv/dist/index.cjs:478:33)\n    at Cacheable.get (/home/nodejs/app/node_modules/cacheable/dist/index.cjs:1378:36)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

How To Reproduce (best to provide workable code or tests!)

Call CacheableMemory.get() > 17M times with different values.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions