I am asking from a NodeJS standpoint and specifically for lru-cache, but the question should apply broadly. Almost every memory cache manager I could find refers to "size", "maxSize" or something similar but never mentions what kind of unit that is. Is it a number of key/value pairs? Is it a number in bytes? Or something else entirely?
An additional question would be how would I calculate the amount of memory one key/value takes up? For example:
('key', {name: 'John', age: '55'})
Side note: not sure if this is relevant, but the reason I am asking is that I want to calculate theoretical cache limits with the available resources that my environment has. And after I know the theoretical limit I can do a practical one to see if it maps on and get the actual limit.
CodePudding user response:
When dealing with a cache the size is usually in bytes. For example redis has a max key/value size of 512mb by default: https://linuxhint.com/redis-max-key-length/
For your use-case it's a little bit different since lru-cache has a method you can define for the sizeCalculation: https://github.com/isaacs/node-lru-cache#sizecalculation
To calculate the size of an object like {name: 'John', age: '55'}
in bytes you could do the following in NodeJS:
sizeCalculation: (value, key) => {
return Buffer.byteLength(JSON.stringify(value))
},