14
14
* limitations under the License.
15
15
*/
16
16
17
- import LRUCacheElement from "./LRUCacheElement "
17
+ import CacheElement from "./CacheElement "
18
18
19
19
/**
20
20
* Least-Recently Used Cache (LRU Cache) Implementation with Generic Key-Value Pairs
@@ -23,17 +23,17 @@ import LRUCacheElement from "./LRUCacheElement"
23
23
* - Removes stale elements (entries older than their timeout) from the cache.
24
24
*/
25
25
export class LRUCache < K , V > {
26
- private _map : Map < K , LRUCacheElement < V > > = new Map ( )
27
- private _maxSize = 100 // Defines maximum size of _map
28
- private _timeout = 1000 * 600 // Milliseconds each entry has before it becomes stale
26
+ private _map : Map < K , CacheElement < V > > = new Map ( )
27
+ private _maxSize // Defines maximum size of _map
28
+ private _timeout // Milliseconds each entry has before it becomes stale
29
29
30
- get map ( ) : Map < K , LRUCacheElement < V > > { return this . _map }
30
+ get map ( ) : Map < K , CacheElement < V > > { return this . _map }
31
31
get maxSize ( ) : number { return this . _maxSize }
32
32
get timeout ( ) : number { return this . _timeout }
33
33
34
- constructor ( { maxSize, timeout } : { maxSize ? : number , timeout ? : number } ) {
35
- if ( maxSize != undefined ) this . _maxSize = maxSize
36
- if ( timeout != undefined ) this . _timeout = timeout
34
+ constructor ( { maxSize, timeout } : { maxSize : number , timeout : number } ) {
35
+ this . _maxSize = maxSize
36
+ this . _timeout = timeout
37
37
}
38
38
39
39
/**
@@ -43,19 +43,18 @@ export class LRUCache<K, V> {
43
43
public lookup ( key : K ) : V | null {
44
44
if ( this . _maxSize <= 0 ) { return null }
45
45
46
- const element : LRUCacheElement < V > | undefined = this . _map . get ( key )
46
+ const element : CacheElement < V > | undefined = this . _map . get ( key )
47
47
48
48
if ( ! element ) return null
49
49
50
- this . _map . delete ( key )
51
- this . _map . set ( key , element )
52
-
53
-
54
50
if ( element . is_stale ( this . _timeout ) ) {
55
51
this . _map . delete ( key )
56
52
return null
57
53
}
58
54
55
+ this . _map . delete ( key )
56
+ this . _map . set ( key , element )
57
+
59
58
return element . value
60
59
}
61
60
@@ -66,9 +65,9 @@ export class LRUCache<K, V> {
66
65
public save ( { key, value } : { key : K , value : V } ) : void {
67
66
if ( this . _maxSize <= 0 ) return
68
67
69
- const element : LRUCacheElement < V > | undefined = this . _map . get ( key )
68
+ const element : CacheElement < V > | undefined = this . _map . get ( key )
70
69
if ( element ) this . _map . delete ( key )
71
- this . _map . set ( key , new LRUCacheElement ( value ) )
70
+ this . _map . set ( key , new CacheElement ( value ) )
72
71
73
72
if ( this . _map . size > this . _maxSize ) {
74
73
const firstMapEntryKey = this . _map . keys ( ) . next ( ) . value
@@ -87,18 +86,12 @@ export class LRUCache<K, V> {
87
86
88
87
/**
89
88
* Reads value from specified key without moving elements in the LRU Cache.
90
- * Removes the element if it is stale.
91
89
* @param {K } key
92
90
*/
93
91
public peek ( key : K ) : V | null {
94
92
if ( this . _maxSize <= 0 ) return null
95
93
96
- const element : LRUCacheElement < V > | undefined = this . _map . get ( key )
97
-
98
- if ( element ?. is_stale ( this . _timeout ) ) {
99
- this . _map . delete ( key )
100
- return null
101
- }
94
+ const element : CacheElement < V > | undefined = this . _map . get ( key )
102
95
103
96
return element ?. value ?? null
104
97
}
0 commit comments