1616
1717import { getTimeInNanos } from "./utils" ;
1818import { MapUtils } from "./map_utils" ;
19-
20- class CacheItem < V > {
21- readonly item : V ;
22- private _expirationTimeNanos : bigint ;
23-
24- constructor ( item : V , expirationTimeNanos : bigint ) {
25- this . item = item ;
26- this . _expirationTimeNanos = expirationTimeNanos ;
27- }
28-
29- get expirationTimeNs ( ) : bigint {
30- return this . _expirationTimeNanos ;
31- }
32-
33- updateExpiration ( expirationIntervalNanos : bigint ) : CacheItem < V > {
34- this . _expirationTimeNanos = getTimeInNanos ( ) + expirationIntervalNanos ;
35- return this ;
36- }
37- }
19+ import { CacheItem } from "./cache_map" ;
3820
3921export class SlidingExpirationCache < K , V > {
4022 protected _cleanupIntervalNanos : bigint = BigInt ( 10 * 60_000_000_000 ) ; // 10 minutes
@@ -69,29 +51,29 @@ export class SlidingExpirationCache<K, V> {
6951 computeIfAbsent ( key : K , mappingFunc : ( key : K ) => V , itemExpirationNanos : bigint ) : V | null {
7052 this . cleanUp ( ) ;
7153 const cacheItem = MapUtils . computeIfAbsent ( this . map , key , ( k ) => new CacheItem ( mappingFunc ( k ) , getTimeInNanos ( ) + itemExpirationNanos ) ) ;
72- return cacheItem ?. updateExpiration ( itemExpirationNanos ) . item ?? null ;
54+ return cacheItem ?. updateExpiration ( itemExpirationNanos ) . get ( ) ?? null ;
7355 }
7456
7557 putIfAbsent ( key : K , value : V , itemExpirationNanos : bigint ) : V | null {
7658 const cacheItem = MapUtils . putIfAbsent ( this . map , key , new CacheItem ( value , getTimeInNanos ( ) + itemExpirationNanos ) ) ;
77- return cacheItem ?. item ?? null ;
59+ return cacheItem ?. get ( true ) ?? null ;
7860 }
7961
8062 get ( key : K , itemExpirationNanos ?: bigint ) : V | undefined {
8163 this . cleanUp ( ) ;
8264 const cacheItem = this . map . get ( key ) ;
83- if ( cacheItem ?. item && itemExpirationNanos ) {
65+ if ( cacheItem ?. get ( true ) && itemExpirationNanos ) {
8466 cacheItem . updateExpiration ( itemExpirationNanos ) ;
85- return cacheItem . item ;
67+ return cacheItem . get ( ) ;
8668 }
87- return cacheItem ?. item ;
69+ return cacheItem ?. get ( true ) ;
8870 }
8971
9072 put ( key : K , value : V , itemExpirationNanos : bigint ) : V | null {
9173 this . cleanUp ( ) ;
9274 const cacheItem = new CacheItem ( value , getTimeInNanos ( ) + itemExpirationNanos ) ;
9375 this . map . set ( key , cacheItem ) ;
94- return cacheItem . item ;
76+ return cacheItem . get ( true ) ;
9577 }
9678
9779 remove ( key : K ) : void {
@@ -102,15 +84,15 @@ export class SlidingExpirationCache<K, V> {
10284 removeAndDispose ( key : K ) : void {
10385 const cacheItem = MapUtils . remove ( this . map , key ) ;
10486 if ( cacheItem != null && this . _itemDisposalFunc != null ) {
105- this . _itemDisposalFunc ( cacheItem . item ) ;
87+ this . _itemDisposalFunc ( cacheItem . get ( true ) ) ;
10688 }
10789 }
10890
10991 removeIfExpired ( key : K ) : void {
11092 let item ;
11193 MapUtils . computeIfPresent ( this . map , key , ( key , cacheItem ) => {
11294 if ( this . shouldCleanupItem ( cacheItem ) ) {
113- item = cacheItem . item ;
95+ item = cacheItem . get ( true ) ;
11496 return null ;
11597 }
11698 return cacheItem ;
@@ -123,15 +105,15 @@ export class SlidingExpirationCache<K, V> {
123105
124106 shouldCleanupItem ( cacheItem : CacheItem < V > ) : boolean {
125107 if ( this . _shouldDisposeFunc != null ) {
126- return getTimeInNanos ( ) > cacheItem . expirationTimeNs && this . _shouldDisposeFunc ( cacheItem . item ) ;
108+ return cacheItem . isExpired ( ) && this . _shouldDisposeFunc ( cacheItem . get ( true ) ) ;
127109 }
128- return getTimeInNanos ( ) > cacheItem . expirationTimeNs ;
110+ return cacheItem . isExpired ( ) ;
129111 }
130112
131113 clear ( ) : void {
132114 for ( const [ key , val ] of this . map . entries ( ) ) {
133115 if ( val !== undefined && this . _itemDisposalFunc !== undefined ) {
134- this . _itemDisposalFunc ( val . item ) ;
116+ this . _itemDisposalFunc ( val . get ( ) ) ;
135117 }
136118 }
137119 this . map . clear ( ) ;
0 commit comments