Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.0.0
Choose a base ref
...
head repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.0.1
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Nov 21, 2018

  1. Verified

    This commit was signed with the committer’s verified signature.
    isaacs isaacs
    Copy the full SHA
    1a4be6b View commit details
  2. 5.0.1

    isaacs committed Nov 21, 2018

    Verified

    This commit was signed with the committer’s verified signature.
    isaacs isaacs
    Copy the full SHA
    54a9dc4 View commit details
Showing with 27 additions and 21 deletions.
  1. +6 −3 README.md
  2. +10 −10 index.js
  3. +1 −1 package-lock.json
  4. +1 −1 package.json
  5. +9 −6 test/basic.js
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ var LRU = require("lru-cache")
, length: function (n, key) { return n * 2 + key.length }
, dispose: function (key, n) { n.close() }
, maxAge: 1000 * 60 * 60 }
, cache = LRU(options)
, otherCache = LRU(50) // sets just the max size
, cache = new LRU(options)
, otherCache = new LRU(50) // sets just the max size

cache.set("key", "value")
cache.get("key") // "value"
@@ -49,10 +49,13 @@ away.
* `max` The maximum size of the cache, checked by applying the length
function to all values in the cache. Not setting this is kind of
silly, since that's the whole purpose of this lib, but it defaults
to `Infinity`.
to `Infinity`. Setting it to a non-number or negative number will
throw a `TypeError`. Setting it to 0 makes it be `Infinity`.
* `maxAge` Maximum age in ms. Items are not pro-actively pruned out
as they age, but if you try to get an item that is too old, it'll
drop it and return undefined instead of giving it to you.
Setting this to a negative value will make everything seem old!
Setting it to a non-number will throw a `TypeError`.
* `length` Function that is used to calculate the length of stored
items. If you're storing strings or buffers, then you probably want
to do something like `function(n, key){return n.length}`. The default is
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -31,16 +31,16 @@ class LRUCache {
if (!options)
options = {}

const max = this[MAX] = options.max
if (options.max && (typeof options.max !== 'number' || options.max < 0))
throw new TypeError('max must be a non-negative number')
// Kind of weird to have a default max of Infinity, but oh well.
if (!max ||
!(typeof max === 'number') ||
max <= 0)
this[MAX] = Infinity
const max = this[MAX] = options.max || Infinity

const lc = options.length || naiveLength
this[LENGTH_CALCULATOR] = (typeof lc !== 'function') ? naiveLength : lc
this[ALLOW_STALE] = options.stale || false
if (options.maxAge && typeof options.maxAge !== 'number')
throw new TypeError('maxAge must be a number')
this[MAX_AGE] = options.maxAge || 0
this[DISPOSE] = options.dispose
this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false
@@ -49,10 +49,10 @@ class LRUCache {

// resize the cache when the max changes.
set max (mL) {
if (!mL || !(typeof mL === 'number') || mL <= 0)
mL = Infinity
if (typeof mL !== 'number' || mL < 0)
throw new TypeError('max must be a non-negative number')

this[MAX] = mL
this[MAX] = mL || Infinity
trim(this)
}
get max () {
@@ -67,8 +67,8 @@ class LRUCache {
}

set maxAge (mA) {
if (!mA || !(typeof mA === 'number') || mA < 0)
mA = 0
if (typeof mA !== 'number')
throw new TypeError('maxAge must be a non-negative number')

this[MAX_AGE] = mA
trim(this)
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lru-cache",
"description": "A cache object that deletes the least-recently-used items.",
"version": "5.0.0",
"version": "5.0.1",
"author": "Isaac Z. Schlueter <i@izs.me>",
"keywords": [
"mru",
15 changes: 9 additions & 6 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ test('max', function (t) {
}

// now remove the max restriction, and try again.
cache.max = 'hello'
cache.max = 0
for (i = 0; i < 100; i++) cache.set(i, i)
t.equal(cache.length, 100)
for (i = 0; i < 100; i++) {
@@ -520,11 +520,6 @@ test('maxAge on list, cleared in forEach', function (t) {
// hacky. make it seem older.
l.dumpLru().head.value.now = Date.now() - 100000

// setting maxAge to invalid values does nothing.
t.equal(l.maxAge, 0)
l.maxAge = -100
t.equal(l.maxAge, 0)
l.maxAge = {}
t.equal(l.maxAge, 0)

l.maxAge = 1
@@ -539,3 +534,11 @@ test('maxAge on list, cleared in forEach', function (t) {

t.end()
})

test('bad max/maxAge options', t => {
t.throws(() => new LRU({ maxAge: true }), 'maxAge must be a number')
t.throws(() => { new LRU().maxAge = 'foo' }, 'maxAge must be a number')
t.throws(() => new LRU({ max: true }), 'max must be a non-negative number')
t.throws(() => { new LRU().max = 'foo' }, 'max must be a non-negative number')
t.end()
})