Browse Source

lru cache set returns removed entry

Alexander Rose 5 years ago
parent
commit
524ed90e3f
1 changed files with 6 additions and 3 deletions
  1. 6 3
      src/mol-util/lru-cache.ts

+ 6 - 3
src/mol-util/lru-cache.ts

@@ -43,11 +43,14 @@ namespace LRUCache {
         return void 0;
     }
 
-    export function set<T>(cache: LRUCache<T>, key: string, data: T): T {
+    export function set<T>(cache: LRUCache<T>, key: string, data: T): T | undefined {
+        let removed: T | undefined = undefined;
         if (cache.entries.count >= cache.capacity) {
-            cache.entries.remove(cache.entries.first!);
+            const first = cache.entries.first!;
+            removed = first.value.data;
+            cache.entries.remove(first);
         }
         cache.entries.addLast(entry(key, data));
-        return data;
+        return removed;
     }
 }