Skip to content

Commit 3a71a30

Browse files
committed
Correct remainingTTL reported on status object
Fix: #293 Sometimes basic addition and subtraction is really hard.
1 parent 4134a83 commit 3a71a30

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,8 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
11671167
status.ttl = ttl
11681168
status.start = start
11691169
status.now = cachedNow || getNow()
1170-
status.remainingTTL = status.now + ttl - start
1170+
const age = status.now - start
1171+
status.remainingTTL = ttl - age
11711172
}
11721173
}
11731174

@@ -1197,9 +1198,13 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
11971198
if (index === undefined) {
11981199
return 0
11991200
}
1200-
return ttls[index] === 0 || starts[index] === 0
1201-
? Infinity
1202-
: starts[index] + ttls[index] - (cachedNow || getNow())
1201+
const ttl = ttls[index]
1202+
const start = starts[index]
1203+
if (ttl === 0 || start === 0) {
1204+
return Infinity
1205+
}
1206+
const age = (cachedNow || getNow()) - start
1207+
return ttl - age
12031208
}
12041209

12051210
this.#isStale = index => {

tap-snapshots/test/ttl.ts.test.cjs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@ Array [
4545
Object {
4646
"get": "hit",
4747
"now": 1969,
48-
"remainingTTL": 15,
48+
"remainingTTL": 5,
4949
"start": 1964,
5050
"ttl": 10,
5151
},
5252
Object {
5353
"get": "hit",
5454
"now": 1974,
55-
"remainingTTL": 20,
55+
"remainingTTL": 0,
5656
"start": 1964,
5757
"ttl": 10,
5858
},
5959
Object {
6060
"has": "stale",
6161
"now": 1975,
62-
"remainingTTL": 21,
62+
"remainingTTL": -1,
6363
"start": 1964,
6464
"ttl": 10,
6565
},
6666
Object {
6767
"get": "stale",
6868
"now": 1975,
69-
"remainingTTL": 21,
69+
"remainingTTL": -1,
7070
"start": 1964,
7171
"ttl": 10,
7272
},
@@ -80,28 +80,28 @@ Array [
8080
Object {
8181
"has": "hit",
8282
"now": 2025,
83-
"remainingTTL": 150,
83+
"remainingTTL": 50,
8484
"start": 1975,
8585
"ttl": 100,
8686
},
8787
Object {
8888
"get": "hit",
8989
"now": 2025,
90-
"remainingTTL": 150,
90+
"remainingTTL": 50,
9191
"start": 1975,
9292
"ttl": 100,
9393
},
9494
Object {
9595
"has": "stale",
9696
"now": 2076,
97-
"remainingTTL": 201,
97+
"remainingTTL": -1,
9898
"start": 1975,
9999
"ttl": 100,
100100
},
101101
Object {
102102
"get": "stale",
103103
"now": 2076,
104-
"remainingTTL": 201,
104+
"remainingTTL": -1,
105105
"start": 1975,
106106
"ttl": 100,
107107
},
@@ -171,14 +171,14 @@ Array [
171171
Object {
172172
"has": "stale",
173173
"now": 2087,
174-
"remainingTTL": 21,
174+
"remainingTTL": -1,
175175
"start": 2076,
176176
"ttl": 10,
177177
},
178178
Object {
179179
"get": "stale",
180180
"now": 2087,
181-
"remainingTTL": 21,
181+
"remainingTTL": -1,
182182
"start": 2076,
183183
"ttl": 10,
184184
},
@@ -204,49 +204,49 @@ Array [
204204
Object {
205205
"get": "hit",
206206
"now": 1522,
207-
"remainingTTL": 15,
207+
"remainingTTL": 5,
208208
"start": 1517,
209209
"ttl": 10,
210210
},
211211
Object {
212212
"get": "hit",
213213
"now": 1527,
214-
"remainingTTL": 20,
214+
"remainingTTL": 0,
215215
"start": 1517,
216216
"ttl": 10,
217217
},
218218
Object {
219219
"has": "stale",
220220
"now": 1529,
221-
"remainingTTL": 22,
221+
"remainingTTL": -2,
222222
"start": 1517,
223223
"ttl": 10,
224224
},
225225
Object {
226226
"get": "stale",
227227
"now": 1529,
228-
"remainingTTL": 22,
228+
"remainingTTL": -2,
229229
"start": 1517,
230230
"ttl": 10,
231231
},
232232
Object {
233233
"has": "hit",
234234
"now": 1579,
235-
"remainingTTL": 150,
235+
"remainingTTL": 50,
236236
"start": 1529,
237237
"ttl": 100,
238238
},
239239
Object {
240240
"get": "hit",
241241
"now": 1579,
242-
"remainingTTL": 150,
242+
"remainingTTL": 50,
243243
"start": 1529,
244244
"ttl": 100,
245245
},
246246
Object {
247247
"get": "stale",
248248
"now": 1630,
249-
"remainingTTL": 201,
249+
"remainingTTL": -1,
250250
"start": 1529,
251251
"ttl": 100,
252252
},
@@ -316,14 +316,14 @@ Array [
316316
Object {
317317
"has": "stale",
318318
"now": 1641,
319-
"remainingTTL": 21,
319+
"remainingTTL": -1,
320320
"start": 1630,
321321
"ttl": 10,
322322
},
323323
Object {
324324
"get": "stale",
325325
"now": 1641,
326-
"remainingTTL": 21,
326+
"remainingTTL": -1,
327327
"start": 1630,
328328
"ttl": 10,
329329
},
@@ -383,14 +383,14 @@ Array [
383383
Object {
384384
"has": "stale",
385385
"now": 1952,
386-
"remainingTTL": 121,
386+
"remainingTTL": -101,
387387
"start": 1841,
388388
"ttl": 10,
389389
},
390390
Object {
391391
"get": "stale",
392392
"now": 1952,
393-
"remainingTTL": 121,
393+
"remainingTTL": -101,
394394
"start": 1841,
395395
"ttl": 10,
396396
},
@@ -464,28 +464,28 @@ Array [
464464
Object {
465465
"get": "hit",
466466
"now": 453,
467-
"remainingTTL": 15,
467+
"remainingTTL": 5,
468468
"start": 448,
469469
"ttl": 10,
470470
},
471471
Object {
472472
"get": "hit",
473473
"now": 458,
474-
"remainingTTL": 20,
474+
"remainingTTL": 0,
475475
"start": 448,
476476
"ttl": 10,
477477
},
478478
Object {
479479
"has": "stale",
480480
"now": 459,
481-
"remainingTTL": 21,
481+
"remainingTTL": -1,
482482
"start": 448,
483483
"ttl": 10,
484484
},
485485
Object {
486486
"get": "stale",
487487
"now": 459,
488-
"remainingTTL": 21,
488+
"remainingTTL": -1,
489489
"start": 448,
490490
"ttl": 10,
491491
},
@@ -499,28 +499,28 @@ Array [
499499
Object {
500500
"has": "hit",
501501
"now": 509,
502-
"remainingTTL": 150,
502+
"remainingTTL": 50,
503503
"start": 459,
504504
"ttl": 100,
505505
},
506506
Object {
507507
"get": "hit",
508508
"now": 509,
509-
"remainingTTL": 150,
509+
"remainingTTL": 50,
510510
"start": 459,
511511
"ttl": 100,
512512
},
513513
Object {
514514
"has": "stale",
515515
"now": 560,
516-
"remainingTTL": 201,
516+
"remainingTTL": -1,
517517
"start": 459,
518518
"ttl": 100,
519519
},
520520
Object {
521521
"get": "stale",
522522
"now": 560,
523-
"remainingTTL": 201,
523+
"remainingTTL": -1,
524524
"start": 459,
525525
"ttl": 100,
526526
},
@@ -590,14 +590,14 @@ Array [
590590
Object {
591591
"has": "stale",
592592
"now": 571,
593-
"remainingTTL": 21,
593+
"remainingTTL": -1,
594594
"start": 560,
595595
"ttl": 10,
596596
},
597597
Object {
598598
"get": "stale",
599599
"now": 571,
600-
"remainingTTL": 21,
600+
"remainingTTL": -1,
601601
"start": 560,
602602
"ttl": 10,
603603
},
@@ -623,49 +623,49 @@ Array [
623623
Object {
624624
"get": "hit",
625625
"now": 6,
626-
"remainingTTL": 15,
626+
"remainingTTL": 5,
627627
"start": 1,
628628
"ttl": 10,
629629
},
630630
Object {
631631
"get": "hit",
632632
"now": 11,
633-
"remainingTTL": 20,
633+
"remainingTTL": 0,
634634
"start": 1,
635635
"ttl": 10,
636636
},
637637
Object {
638638
"has": "stale",
639639
"now": 13,
640-
"remainingTTL": 22,
640+
"remainingTTL": -2,
641641
"start": 1,
642642
"ttl": 10,
643643
},
644644
Object {
645645
"get": "stale",
646646
"now": 13,
647-
"remainingTTL": 22,
647+
"remainingTTL": -2,
648648
"start": 1,
649649
"ttl": 10,
650650
},
651651
Object {
652652
"has": "hit",
653653
"now": 63,
654-
"remainingTTL": 150,
654+
"remainingTTL": 50,
655655
"start": 13,
656656
"ttl": 100,
657657
},
658658
Object {
659659
"get": "hit",
660660
"now": 63,
661-
"remainingTTL": 150,
661+
"remainingTTL": 50,
662662
"start": 13,
663663
"ttl": 100,
664664
},
665665
Object {
666666
"get": "stale",
667667
"now": 114,
668-
"remainingTTL": 201,
668+
"remainingTTL": -1,
669669
"start": 13,
670670
"ttl": 100,
671671
},
@@ -735,14 +735,14 @@ Array [
735735
Object {
736736
"has": "stale",
737737
"now": 125,
738-
"remainingTTL": 21,
738+
"remainingTTL": -1,
739739
"start": 114,
740740
"ttl": 10,
741741
},
742742
Object {
743743
"get": "stale",
744744
"now": 125,
745-
"remainingTTL": 21,
745+
"remainingTTL": -1,
746746
"start": 114,
747747
"ttl": 10,
748748
},
@@ -802,14 +802,14 @@ Array [
802802
Object {
803803
"has": "stale",
804804
"now": 436,
805-
"remainingTTL": 121,
805+
"remainingTTL": -101,
806806
"start": 325,
807807
"ttl": 10,
808808
},
809809
Object {
810810
"get": "stale",
811811
"now": 436,
812-
"remainingTTL": 121,
812+
"remainingTTL": -101,
813813
"start": 325,
814814
"ttl": 10,
815815
},

0 commit comments

Comments
 (0)