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.1
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.1.0
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Nov 21, 2018

  1. Add updateAgeOnGet option

    Close #119
    isaacs committed Nov 21, 2018
    Copy the full SHA
    fc66d5a View commit details
  2. 5.1.0

    isaacs committed Nov 21, 2018
    4
    Copy the full SHA
    3c2af5a View commit details
Showing with 30 additions and 3 deletions.
  1. +5 −0 README.md
  2. +6 −1 index.js
  3. +1 −1 package-lock.json
  4. +1 −1 package.json
  5. +17 −0 test/basic.js
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -79,6 +79,11 @@ away.
it'll be called whenever a `set()` operation overwrites an existing
key. If you set this option, `dispose()` will only be called when a
key falls out of the cache, not when it is overwritten.
* `updateAgeOnGet` When using time-expiring entries with `maxAge`,
setting this to `true` will make each item's effective time update
to the current time whenever it is retrieved from cache, causing it
to not expire. (It can still fall out of cache based on recency of
use, of course.)

## API

7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ const DISPOSE = Symbol('dispose')
const NO_DISPOSE_ON_SET = Symbol('noDisposeOnSet')
const LRU_LIST = Symbol('lruList')
const CACHE = Symbol('cache')
const UPDATE_AGE_ON_GET = Symbol('updateAgeOnGet')

const naiveLength = () => 1

@@ -44,6 +45,7 @@ class LRUCache {
this[MAX_AGE] = options.maxAge || 0
this[DISPOSE] = options.dispose
this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false
this[UPDATE_AGE_ON_GET] = options.updateAgeOnGet || false
this.reset()
}

@@ -260,8 +262,11 @@ const get = (self, key, doUse) => {
if (!self[ALLOW_STALE])
return undefined
} else {
if (doUse)
if (doUse) {
if (self[UPDATE_AGE_ON_GET])
node.value.now = Date.now()
self[LRU_LIST].unshiftNode(node)
}
}
return hit.value
}
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.1",
"version": "5.1.0",
"author": "Isaac Z. Schlueter <i@izs.me>",
"keywords": [
"mru",
17 changes: 17 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -542,3 +542,20 @@ test('bad max/maxAge options', t => {
t.throws(() => { new LRU().max = 'foo' }, 'max must be a non-negative number')
t.end()
})

test('update age on get', t => {
const l = new LRU({ updateAgeOnGet: true, maxAge: 10 })
l.set('foo', 'bar')
const e1 = l.dump()[0].e
// spin for 5ms
for (let then = Date.now() + 5; then > Date.now(); );
l.get('foo')
const e2 = l.dump()[0].e
// spin for 5ms
for (let then = Date.now() + 5; then > Date.now(); );
l.get('foo')
const e3 = l.dump()[0].e
t.ok(e1 < e2, 'time updated on first get')
t.ok(e2 < e3, 'time updated on second get')
t.end()
})