Memory store: how to set the max value to infinite (only allocate memory on new items)
Thomas-1985 opened this issue · 4 comments
Hello,
npm version: 5.3.2
Node.js version: 16.16.0
Typescript: 5.3.3
I use cache-manager in my project and i want to use the builtin memory-cache. I don't want expiration of keys so i set ttl: 0
.
However, i also don't know in advance how much cache entries there are. When i set the max
value too low, i cannot save all values, when i set it too high, the node process runs out of memory (i think the memory is allocated when the cache-manager instance is created).
How can i solve this (i want that the cache size grows automatically when new values are added)?
Best,
Thomas
Hi @Thomas-1985 - first, I would highly recommend that you do set a ttl as that is what is best practice and you will hit the issue of running out of memory.
If you are only doing in memory and dont want to set the max
just use a standard LRU type cache and do a calculation on the size of the server and the objects you are putting in.
But how can i avoid setting a ttl if the cache should be persistent? In my case, it is filled upon startup of my app and updated if neccessary. In other cases the data doesn't change and has to be up to date in the cache, thats why i didn't set a ttl (and also because filling the cache takes some time).
If you dont want to set a ttl just use Map()
But if i want to use your module (because of the different cache engines) and set a max
but don't set a ttl
, will it then run out of memory (in this case i don't understand why - please explain)?