From 6234e88265deb6c956ee7e17f8c7332578fb7532 Mon Sep 17 00:00:00 2001
From: HiccupHan <89772977+HiccupHan@users.noreply.github.com>
Date: Thu, 13 Feb 2025 13:37:27 -0800
Subject: [PATCH] prefetch init

---
 cache.py          |  3 ++-
 prefetch_cache.py | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 prefetch_cache.py

diff --git a/cache.py b/cache.py
index e0dc67d..e547178 100644
--- a/cache.py
+++ b/cache.py
@@ -56,7 +56,8 @@ class BaselineCache(Cache):
             evict = True
         
         self.cache[key] = val
-        self.cache.move_to_end(key)
+        # no need for this since this op appends key-val by default
+        # self.cache.move_to_end(key)
 
         return evict
 
diff --git a/prefetch_cache.py b/prefetch_cache.py
new file mode 100644
index 0000000..f5bd3f8
--- /dev/null
+++ b/prefetch_cache.py
@@ -0,0 +1,28 @@
+from cache import BaselineCache
+
+class PrefetchCache(BaselineCache):
+    key_relations = None
+    
+    def __init__(self):
+        super()
+        self.key_relations = dict()
+        
+    def put(self, key: str, val: str) -> bool:
+        # LRU evict
+        evict = False
+        if len(self.cache) >= self.limit:
+            self.cache.popitem(last = False)
+            evict = True
+        self.cache[key] = val   
+        self.prefetch(key, val)
+        
+        return evict
+    
+    def prefetch(self, key: str, val: str) -> bool:
+        if len(self.cache) >= self.limit and key in self.key_relations:
+            self.cache[self.key_relations[key][0]] = self.key_relations[key][1]
+            return True
+        return False
+
+    def set_relations(self):
+        return
\ No newline at end of file