Skip to content

Commit

Permalink
Correct remainingTTL reported on status object
Browse files Browse the repository at this point in the history
Fix: #293

Sometimes basic addition and subtraction is really hard.
  • Loading branch information
isaacs committed Apr 23, 2023
1 parent 4134a83 commit 3a71a30
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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 => {
Expand Down
84 changes: 42 additions & 42 deletions tap-snapshots/test/ttl.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down

0 comments on commit 3a71a30

Please sign in to comment.