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

Size is required in cache.set #219

Closed yuvalshilo closed 2 years ago

yuvalshilo commented 2 years ago

in LRUCache.SetOptions (input of 'set'), the size is required, and if not passed - an error is thrown "size must be positive integer"

isaacs commented 2 years ago

Where are you seeing that size option is required? It is not required.

nazar-i commented 2 years ago

@isaacs, I'am facing the same issue. Example code:

lru-cache@7.7.1 node v14.16.0

example.js:

const Cache = require('lru-cache');

const cache = new Cache({max: 10, maxSize: 10000});

try {
    cache.set(1, '1');
} catch (err) {
    console.error(err);
}

In console:

TypeError: size must be positive integer
    at LRUCache.requireSize (/home/user/project-path/node_modules/lru-cache/index.js:281:15)
    at LRUCache.set (/home/user/project-path/node_modules/lru-cache/index.js:457:17)
    at Object.<anonymous> (/home/user/project-path/lru.js:6:8)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

btw, example from README throw the same error if i comment custom sizeCalculation implementation. UPD. Well, size is required, but only when ... README ref: When maxSize is set, every item must provide a size, either via the sizeCalculation method provided to the constructor, or via a size or sizeCalculation option provided to cache.set(). The size of every item must be a positive integer. @yuvalshilo, is it your case?