isaacs / node-lru-cache

A fast cache that automatically deletes the least recently used items
http://isaacs.github.io/node-lru-cache/
ISC License
5.38k stars 353 forks source link

Question: I am confused if I need to implement length and dispose manually or will it just work? #333

Closed touhidurrr closed 7 months ago

touhidurrr commented 7 months ago

Note: I am using string as key and value type. This is my setup:

import bytes from 'bytes';
import { LRUCache } from 'lru-cache';

export const cache = new LRUCache<string, string>({
  max: 1000,
  maxSize: bytes.parse('50mb'),
  sizeCalculation: val => val.length,
});

Do I have to set anything else? Or this will work just fine. I read the docs and confused what is optional and what is necessary and how will the cache behave with the setup above.

isaacs commented 7 months ago

That should work just fine. It'll store up to 50 megabytes of character values. (Since string is used for both key and value, maybe you want to add the length of the key and length of the value? Unless the keys are always very short, then maybe doesn't matter much.)

touhidurrr commented 7 months ago

Thanks @isaacs for replying. But I think the docs might be a little bit confusing. Would appreciate if all properties are explained in this format: image I mean, explaining what the default behavior is when some option is not used more clearly.

touhidurrr commented 7 months ago

@isaacs, i am not sure if these settings are working properly. Screenshot_2024-04-16-04-23-34-81_d073e7407281f7a2d9df7c9f755e6603

https://github.com/touhidurrr/UncivServer.xyz/blob/41017af3f103f497e46b9fa99948f160d6dc713d/src/constants.ts#L19-L20

https://github.com/touhidurrr/UncivServer.xyz/blob/41017af3f103f497e46b9fa99948f160d6dc713d/src/services/lrucache.ts

[!NOTE] My keys are UUIDs and length can be ignored. Also the point where the memory started to grow, is the point I started to use lru-cache. Also, my maxSize is 100mb now.