You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -39,21 +43,25 @@ export class Cache<T> implements CacheInterface<T> {
39
43
/**
40
44
* Object that stores all cached entries.
41
45
*/
42
-
privatecache: Record<string,CacheEntry<T>>;
46
+
privatecache: Map<string,CacheEntry<T>>;
43
47
44
48
/**
45
49
* Creates an instance of Cache.
46
50
* @param defaultValidityTime - The default validity time in milliseconds. Use 0 for unlimited cache duration.
51
+
* @param maxSize - The maximum number of entries in the cache. Use Infinity for unlimited size. Items are evicted based on a least recently used (LRU) strategy.
47
52
*/
48
-
constructor(privatedefaultValidityTime: number){
49
-
this.cache={};
53
+
constructor(
54
+
privatedefaultValidityTime: number,
55
+
privatemaxSize=Infinity
56
+
){
57
+
this.cache=newMap<string,CacheEntry<T>>();
50
58
}
51
59
52
60
/**
53
61
* Clear all cached items.
54
62
*/
55
63
clear(): void{
56
-
this.cache={};
64
+
this.cache.clear();
57
65
}
58
66
59
67
/**
@@ -62,7 +70,7 @@ export class Cache<T> implements CacheInterface<T> {
62
70
* @returns A boolean value that indicates whether the entry exists in cache.
63
71
*/
64
72
hasKey(key: string): boolean{
65
-
returnthis.cache.hasOwnProperty(key);
73
+
returnthis.cache.has(key);
66
74
}
67
75
68
76
/**
@@ -71,9 +79,19 @@ export class Cache<T> implements CacheInterface<T> {
71
79
* @returns The corresponding entry to the provided key if it is still valid, returns `undefined` otherwise.
0 commit comments