summaryrefslogtreecommitdiff
path: root/packages/core/src/utils/LruCache.ts
diff options
context:
space:
mode:
authorTommaso Sciortino <[email protected]>2025-05-30 18:25:47 -0700
committerGitHub <[email protected]>2025-05-30 18:25:47 -0700
commit21fba832d1b4ea7af43fb887d9b2b38fcf8210d0 (patch)
tree7200d2fac3a55c385e0a2dac34b5282c942364bc /packages/core/src/utils/LruCache.ts
parentc81148a0cc8489f657901c2cc7247c0834075e1a (diff)
Rename server->core (#638)
Diffstat (limited to 'packages/core/src/utils/LruCache.ts')
-rw-r--r--packages/core/src/utils/LruCache.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/core/src/utils/LruCache.ts b/packages/core/src/utils/LruCache.ts
new file mode 100644
index 00000000..076828c4
--- /dev/null
+++ b/packages/core/src/utils/LruCache.ts
@@ -0,0 +1,41 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+export class LruCache<K, V> {
+ private cache: Map<K, V>;
+ private maxSize: number;
+
+ constructor(maxSize: number) {
+ this.cache = new Map<K, V>();
+ this.maxSize = maxSize;
+ }
+
+ get(key: K): V | undefined {
+ const value = this.cache.get(key);
+ if (value) {
+ // Move to end to mark as recently used
+ this.cache.delete(key);
+ this.cache.set(key, value);
+ }
+ return value;
+ }
+
+ set(key: K, value: V): void {
+ if (this.cache.has(key)) {
+ this.cache.delete(key);
+ } else if (this.cache.size >= this.maxSize) {
+ const firstKey = this.cache.keys().next().value;
+ if (firstKey !== undefined) {
+ this.cache.delete(firstKey);
+ }
+ }
+ this.cache.set(key, value);
+ }
+
+ clear(): void {
+ this.cache.clear();
+ }
+}