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: v9.1.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: v9.1.1
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Apr 23, 2023

  1. Correct remainingTTL reported on status object

    Fix: #293
    
    Sometimes basic addition and subtraction is really hard.
    isaacs committed Apr 23, 2023
    Copy the full SHA
    3a71a30 View commit details
  2. 9.1.1

    isaacs committed Apr 23, 2023
    Copy the full SHA
    7d51bb3 View commit details
Showing with 54 additions and 49 deletions.
  1. +2 −2 package-lock.json
  2. +1 −1 package.json
  3. +9 −4 src/index.ts
  4. +42 −42 tap-snapshots/test/ttl.ts.test.cjs
4 changes: 2 additions & 2 deletions 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": "9.1.0",
"version": "9.1.1",
"author": "Isaac Z. Schlueter <i@izs.me>",
"keywords": [
"mru",
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1167,7 +1167,8 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
status.ttl = ttl
status.start = start
status.now = cachedNow || getNow()
status.remainingTTL = status.now + ttl - start
const age = status.now - start
status.remainingTTL = ttl - age
}
}

@@ -1197,9 +1198,13 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
if (index === undefined) {
return 0
}
return ttls[index] === 0 || starts[index] === 0
? Infinity
: starts[index] + ttls[index] - (cachedNow || getNow())
const ttl = ttls[index]
const start = starts[index]
if (ttl === 0 || start === 0) {
return Infinity
}
const age = (cachedNow || getNow()) - start
return ttl - age
}

this.#isStale = index => {
84 changes: 42 additions & 42 deletions tap-snapshots/test/ttl.ts.test.cjs
Original file line number Diff line number Diff line change
@@ -45,28 +45,28 @@ Array [
Object {
"get": "hit",
"now": 1969,
"remainingTTL": 15,
"remainingTTL": 5,
"start": 1964,
"ttl": 10,
},
Object {
"get": "hit",
"now": 1974,
"remainingTTL": 20,
"remainingTTL": 0,
"start": 1964,
"ttl": 10,
},
Object {
"has": "stale",
"now": 1975,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 1964,
"ttl": 10,
},
Object {
"get": "stale",
"now": 1975,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 1964,
"ttl": 10,
},
@@ -80,28 +80,28 @@ Array [
Object {
"has": "hit",
"now": 2025,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 1975,
"ttl": 100,
},
Object {
"get": "hit",
"now": 2025,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 1975,
"ttl": 100,
},
Object {
"has": "stale",
"now": 2076,
"remainingTTL": 201,
"remainingTTL": -1,
"start": 1975,
"ttl": 100,
},
Object {
"get": "stale",
"now": 2076,
"remainingTTL": 201,
"remainingTTL": -1,
"start": 1975,
"ttl": 100,
},
@@ -171,14 +171,14 @@ Array [
Object {
"has": "stale",
"now": 2087,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 2076,
"ttl": 10,
},
Object {
"get": "stale",
"now": 2087,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 2076,
"ttl": 10,
},
@@ -204,49 +204,49 @@ Array [
Object {
"get": "hit",
"now": 1522,
"remainingTTL": 15,
"remainingTTL": 5,
"start": 1517,
"ttl": 10,
},
Object {
"get": "hit",
"now": 1527,
"remainingTTL": 20,
"remainingTTL": 0,
"start": 1517,
"ttl": 10,
},
Object {
"has": "stale",
"now": 1529,
"remainingTTL": 22,
"remainingTTL": -2,
"start": 1517,
"ttl": 10,
},
Object {
"get": "stale",
"now": 1529,
"remainingTTL": 22,
"remainingTTL": -2,
"start": 1517,
"ttl": 10,
},
Object {
"has": "hit",
"now": 1579,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 1529,
"ttl": 100,
},
Object {
"get": "hit",
"now": 1579,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 1529,
"ttl": 100,
},
Object {
"get": "stale",
"now": 1630,
"remainingTTL": 201,
"remainingTTL": -1,
"start": 1529,
"ttl": 100,
},
@@ -316,14 +316,14 @@ Array [
Object {
"has": "stale",
"now": 1641,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 1630,
"ttl": 10,
},
Object {
"get": "stale",
"now": 1641,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 1630,
"ttl": 10,
},
@@ -383,14 +383,14 @@ Array [
Object {
"has": "stale",
"now": 1952,
"remainingTTL": 121,
"remainingTTL": -101,
"start": 1841,
"ttl": 10,
},
Object {
"get": "stale",
"now": 1952,
"remainingTTL": 121,
"remainingTTL": -101,
"start": 1841,
"ttl": 10,
},
@@ -464,28 +464,28 @@ Array [
Object {
"get": "hit",
"now": 453,
"remainingTTL": 15,
"remainingTTL": 5,
"start": 448,
"ttl": 10,
},
Object {
"get": "hit",
"now": 458,
"remainingTTL": 20,
"remainingTTL": 0,
"start": 448,
"ttl": 10,
},
Object {
"has": "stale",
"now": 459,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 448,
"ttl": 10,
},
Object {
"get": "stale",
"now": 459,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 448,
"ttl": 10,
},
@@ -499,28 +499,28 @@ Array [
Object {
"has": "hit",
"now": 509,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 459,
"ttl": 100,
},
Object {
"get": "hit",
"now": 509,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 459,
"ttl": 100,
},
Object {
"has": "stale",
"now": 560,
"remainingTTL": 201,
"remainingTTL": -1,
"start": 459,
"ttl": 100,
},
Object {
"get": "stale",
"now": 560,
"remainingTTL": 201,
"remainingTTL": -1,
"start": 459,
"ttl": 100,
},
@@ -590,14 +590,14 @@ Array [
Object {
"has": "stale",
"now": 571,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 560,
"ttl": 10,
},
Object {
"get": "stale",
"now": 571,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 560,
"ttl": 10,
},
@@ -623,49 +623,49 @@ Array [
Object {
"get": "hit",
"now": 6,
"remainingTTL": 15,
"remainingTTL": 5,
"start": 1,
"ttl": 10,
},
Object {
"get": "hit",
"now": 11,
"remainingTTL": 20,
"remainingTTL": 0,
"start": 1,
"ttl": 10,
},
Object {
"has": "stale",
"now": 13,
"remainingTTL": 22,
"remainingTTL": -2,
"start": 1,
"ttl": 10,
},
Object {
"get": "stale",
"now": 13,
"remainingTTL": 22,
"remainingTTL": -2,
"start": 1,
"ttl": 10,
},
Object {
"has": "hit",
"now": 63,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 13,
"ttl": 100,
},
Object {
"get": "hit",
"now": 63,
"remainingTTL": 150,
"remainingTTL": 50,
"start": 13,
"ttl": 100,
},
Object {
"get": "stale",
"now": 114,
"remainingTTL": 201,
"remainingTTL": -1,
"start": 13,
"ttl": 100,
},
@@ -735,14 +735,14 @@ Array [
Object {
"has": "stale",
"now": 125,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 114,
"ttl": 10,
},
Object {
"get": "stale",
"now": 125,
"remainingTTL": 21,
"remainingTTL": -1,
"start": 114,
"ttl": 10,
},
@@ -802,14 +802,14 @@ Array [
Object {
"has": "stale",
"now": 436,
"remainingTTL": 121,
"remainingTTL": -101,
"start": 325,
"ttl": 10,
},
Object {
"get": "stale",
"now": 436,
"remainingTTL": 121,
"remainingTTL": -101,
"start": 325,
"ttl": 10,
},