mirror of
https://github.com/ltcptgeneral/cs239-caching.git
synced 2025-10-24 21:09:20 +00:00
Compare commits
7 Commits
bb38d1eeb4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79dec3aec0 | ||
|
|
5e2ce2f442 | ||
|
|
44dd3592b0 | ||
|
|
dbaf99af9e | ||
|
|
db8c5a7130 | ||
|
|
e2d66d2b2a | ||
|
|
88be00d210 |
99
app/cache/eviction_sieve.py
vendored
99
app/cache/eviction_sieve.py
vendored
@@ -11,45 +11,49 @@ class Node:
|
|||||||
class SieveCache(Cache):
|
class SieveCache(Cache):
|
||||||
def __init__(self, limit: int):
|
def __init__(self, limit: int):
|
||||||
super().__init__(limit)
|
super().__init__(limit)
|
||||||
self.limit = limit # Fix: Store limit properly
|
self.limit = limit
|
||||||
self.cache = {} # Hash map for O(1) access
|
self.cache = {} # Hash map for O(1) access
|
||||||
self.head = None
|
self.head = None
|
||||||
self.tail = None
|
self.tail = None
|
||||||
self.hand = None
|
self.hand = None # Pointer for eviction
|
||||||
|
|
||||||
def print_cache_state(self):
|
def invalidate(self, key: str) -> bool:
|
||||||
#print("Current cache state:")
|
"""Removes a specific key from cache if it exists."""
|
||||||
node = self.head
|
if key in self.cache:
|
||||||
if not node:
|
node = self.cache.pop(key)
|
||||||
#print("Cache is empty.")
|
|
||||||
return
|
|
||||||
for _ in range(len(self.cache)):
|
|
||||||
#print(f"Key: {node.key}, Value: {node.value}, Visited: {node.visited}")
|
|
||||||
node = node.next
|
|
||||||
if node == self.head:
|
if node == self.head:
|
||||||
break
|
self.head = node.next
|
||||||
|
if node == self.tail:
|
||||||
|
self.tail = node.prev
|
||||||
|
if node.next:
|
||||||
|
node.next.prev = node.prev
|
||||||
|
if node.prev:
|
||||||
|
node.prev.next = node.next
|
||||||
|
|
||||||
|
return True # Successfully invalidated
|
||||||
|
|
||||||
|
return False # Key not found
|
||||||
|
|
||||||
def get(self, key: str) -> str:
|
def get(self, key: str) -> str:
|
||||||
if key in self.cache:
|
if key in self.cache:
|
||||||
node = self.cache[key]
|
node = self.cache[key]
|
||||||
node.visited = True
|
node.visited = True # Mark node as accessed
|
||||||
#self.print_cache_state()
|
|
||||||
return node.value
|
return node.value
|
||||||
self.print_cache_state()
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def put(self, key: str, val: str) -> bool:
|
def put(self, key: str, val: str) -> bool:
|
||||||
if key in self.cache:
|
if key in self.cache:
|
||||||
node = self.cache[key]
|
node = self.cache[key]
|
||||||
node.value = val
|
node.value = val
|
||||||
node.visited = True
|
node.visited = True
|
||||||
#self.print_cache_state()
|
|
||||||
return False # No eviction needed
|
return False # No eviction needed
|
||||||
|
|
||||||
new_node = Node(key, val)
|
new_node = Node(key, val)
|
||||||
if len(self.cache) >= self.limit:
|
if len(self.cache) >= self.limit:
|
||||||
self.evict()
|
self.evict()
|
||||||
|
|
||||||
|
# Insert new node in circular doubly linked list
|
||||||
if not self.head:
|
if not self.head:
|
||||||
self.head = self.tail = new_node
|
self.head = self.tail = new_node
|
||||||
new_node.next = new_node.prev = new_node
|
new_node.next = new_node.prev = new_node
|
||||||
@@ -62,54 +66,31 @@ class SieveCache(Cache):
|
|||||||
|
|
||||||
self.cache[key] = new_node
|
self.cache[key] = new_node
|
||||||
if not self.hand:
|
if not self.hand:
|
||||||
self.hand = self.head
|
self.hand = self.head # Initialize hand pointer
|
||||||
#self.print_cache_state()
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def invalidate(self, key: str) -> bool:
|
|
||||||
if key in self.cache:
|
|
||||||
node = self.cache.pop(key)
|
|
||||||
if node == self.head:
|
|
||||||
self.head = node.next
|
|
||||||
if node == self.tail:
|
|
||||||
self.tail = node.prev
|
|
||||||
if node.next:
|
|
||||||
node.next.prev = node.prev
|
|
||||||
if node.prev:
|
|
||||||
node.prev.next = node.next
|
|
||||||
#self.print_cache_state()
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def next_hand(self):
|
|
||||||
self.hand = self.hand.next if self.hand.next else self.head
|
|
||||||
|
|
||||||
def evict(self):
|
def evict(self):
|
||||||
|
if not self.hand:
|
||||||
|
return # No elements to evict
|
||||||
|
|
||||||
|
# Find the first unvisited node to evict
|
||||||
while self.hand.visited:
|
while self.hand.visited:
|
||||||
self.hand.visited = False
|
self.hand.visited = False # Reset visited flag
|
||||||
self.next_hand()
|
self.hand = self.hand.next # Move to next node
|
||||||
|
|
||||||
obj_to_evict = self.hand
|
obj_to_evict = self.hand
|
||||||
self.next_hand()
|
self.hand = self.hand.next # Move hand forward
|
||||||
|
|
||||||
|
# Remove from cache dictionary if exists
|
||||||
|
if obj_to_evict.key in self.cache:
|
||||||
|
del self.cache[obj_to_evict.key]
|
||||||
|
|
||||||
|
# Evict the node from linked list
|
||||||
if obj_to_evict == self.head:
|
if obj_to_evict == self.head:
|
||||||
self.head = obj_to_evict.next
|
self.head = obj_to_evict.next
|
||||||
if obj_to_evict == self.tail:
|
if obj_to_evict == self.tail:
|
||||||
self.tail = obj_to_evict.prev
|
self.tail = obj_to_evict.prev
|
||||||
if obj_to_evict.next:
|
|
||||||
obj_to_evict.next.prev = obj_to_evict.prev
|
|
||||||
if obj_to_evict.prev:
|
|
||||||
obj_to_evict.prev.next = obj_to_evict.next
|
|
||||||
|
|
||||||
del self.cache[obj_to_evict.key]
|
|
||||||
#self.print_cache_state()
|
|
||||||
|
|
||||||
# Basic API demo for future testing
|
obj_to_evict.prev.next = obj_to_evict.next
|
||||||
if __name__ == "__main__":
|
obj_to_evict.next.prev = obj_to_evict.prev
|
||||||
cache = SeiveCache(3)
|
|
||||||
cache.put("a", "1")
|
|
||||||
cache.put("b", "2")
|
|
||||||
cache.put("c", "3")
|
|
||||||
cache.get("a")
|
|
||||||
cache.put("d", "4") # Should evict "b"
|
|
||||||
assert "b" not in cache.cache, f"Eviction failed, cache contents: {cache.cache.keys()}"
|
|
||||||
print("SeiveCache eviction test passed.")
|
|
||||||
|
|||||||
@@ -40,33 +40,33 @@ cache throughput (requests / s) : 277.7341979526512
|
|||||||
real throughput (requests / s) : 60.02808337161936
|
real throughput (requests / s) : 60.02808337161936
|
||||||
|
|
||||||
--- weighted_friend_readonly 0.25 Results ---
|
--- weighted_friend_readonly 0.25 Results ---
|
||||||
hits: 2163 misses: 7837 ratio: 0.2163
|
hits: 635 misses: 9365 ratio: 0.0635
|
||||||
average response time (ms) : 10.187475109100342
|
average response time (ms) : 12.026190519332886
|
||||||
average cache hit response time (ms) : 0.08817761792003474
|
average cache hit response time (ms) : 0.0014263813889871433
|
||||||
average cache miss response time (ms): 12.974865752640344
|
average cache miss response time (ms): 12.841537580474837
|
||||||
cache throughput (requests / s) : 98.15974903405778
|
cache throughput (requests / s) : 83.15185082029382
|
||||||
real throughput (requests / s) : 88.35527295489469
|
real throughput (requests / s) : 75.84771942953694
|
||||||
|
|
||||||
--- weighted_friend_readonly 0.50 Results ---
|
--- weighted_friend_readonly 0.50 Results ---
|
||||||
hits: 2766 misses: 7234 ratio: 0.2766
|
hits: 1072 misses: 8928 ratio: 0.1072
|
||||||
average response time (ms) : 9.404127931594848
|
average response time (ms) : 11.465663266181945
|
||||||
average cache hit response time (ms) : 0.07571936170863484
|
average cache hit response time (ms) : 0.001328649805552924
|
||||||
average cache miss response time (ms): 12.97094823907415
|
average cache miss response time (ms): 12.842205236248645
|
||||||
cache throughput (requests / s) : 106.33628203209798
|
cache throughput (requests / s) : 87.21693431809628
|
||||||
real throughput (requests / s) : 95.07378087157565
|
real throughput (requests / s) : 79.27228928100207
|
||||||
|
|
||||||
--- weighted_friend_readonly 0.75 Results ---
|
--- weighted_friend_readonly 0.75 Results ---
|
||||||
hits: 4087 misses: 5913 ratio: 0.4087
|
hits: 2253 misses: 7747 ratio: 0.2253
|
||||||
average response time (ms) : 7.68894898891449
|
average response time (ms) : 9.959305834770202
|
||||||
average cache hit response time (ms) : 0.05545212465887814
|
average cache hit response time (ms) : 0.0012218290786980208
|
||||||
average cache miss response time (ms): 12.965137333952995
|
average cache miss response time (ms): 12.855338268592709
|
||||||
cache throughput (requests / s) : 130.05678688228338
|
cache throughput (requests / s) : 100.40860443393278
|
||||||
real throughput (requests / s) : 114.06672701630119
|
real throughput (requests / s) : 90.2328882960655
|
||||||
|
|
||||||
--- weighted_friend_readonly 1 Results ---
|
--- weighted_friend_readonly 1 Results ---
|
||||||
hits: 9992 misses: 8 ratio: 0.9992
|
hits: 9992 misses: 8 ratio: 0.9992
|
||||||
average response time (ms) : 0.011892271041870118
|
average response time (ms) : 0.01112067699432373
|
||||||
average cache hit response time (ms) : 0.0006238444506215324
|
average cache hit response time (ms) : 0.0008874601894039646
|
||||||
average cache miss response time (ms): 14.086157083511353
|
average cache miss response time (ms): 12.792408466339111
|
||||||
cache throughput (requests / s) : 84088.22810035326
|
cache throughput (requests / s) : 89922.582996559
|
||||||
real throughput (requests / s) : 1066.9048383247582
|
real throughput (requests / s) : 1052.9166753169109
|
||||||
71
tests/results_sieve
Normal file
71
tests/results_sieve
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
--- random_read Results ---
|
||||||
|
hits: 493 misses: 9507 ratio: 0.0493
|
||||||
|
average response time (ms) : 14.870552372932433
|
||||||
|
average cache hit response time (ms) : 0.0013246013958604051
|
||||||
|
average cache miss response time (ms): 15.64161888091261
|
||||||
|
cache throughput (requests / s) : 67.24699761793734
|
||||||
|
real throughput (requests / s) : 59.162448469313915
|
||||||
|
|
||||||
|
--- read_heavy Results ---
|
||||||
|
hits: 431 misses: 7594 ratio: 0.05370716510903427
|
||||||
|
average response time (ms) : 13.949467875875788
|
||||||
|
average cache hit response time (ms) : 0.0010416291982557823
|
||||||
|
average cache miss response time (ms): 14.74111545453236
|
||||||
|
cache throughput (requests / s) : 71.68732233359239
|
||||||
|
real throughput (requests / s) : 55.342634534026395
|
||||||
|
|
||||||
|
--- write_heavy Results ---
|
||||||
|
hits: 106 misses: 1912 ratio: 0.05252725470763132
|
||||||
|
average response time (ms) : 13.415291122686048
|
||||||
|
average cache hit response time (ms) : 0.0009986589539725825
|
||||||
|
average cache miss response time (ms): 14.15897051659588
|
||||||
|
cache throughput (requests / s) : 74.54180389040839
|
||||||
|
real throughput (requests / s) : 24.099083879788104
|
||||||
|
|
||||||
|
--- frequent_users Results ---
|
||||||
|
hits: 7084 misses: 2916 ratio: 0.7084
|
||||||
|
average response time (ms) : 4.243493890762329
|
||||||
|
average cache hit response time (ms) : 0.0009342211912339316
|
||||||
|
average cache miss response time (ms): 14.550178629871258
|
||||||
|
cache throughput (requests / s) : 235.65486972348472
|
||||||
|
real throughput (requests / s) : 168.06057547599738
|
||||||
|
|
||||||
|
--- frequent_after_write Results ---
|
||||||
|
hits: 1008 misses: 3922 ratio: 0.20446247464503042
|
||||||
|
average response time (ms) : 11.290647437074364
|
||||||
|
average cache hit response time (ms) : 0.0008995097780984545
|
||||||
|
average cache miss response time (ms): 14.19224506856713
|
||||||
|
cache throughput (requests / s) : 88.56888017921497
|
||||||
|
real throughput (requests / s) : 49.823185354533955
|
||||||
|
|
||||||
|
--- weighted_friend_readonly 0.25 Results ---
|
||||||
|
hits: 723 misses: 9277 ratio: 0.0723
|
||||||
|
average response time (ms) : 13.950133323669434
|
||||||
|
average cache hit response time (ms) : 0.001212208432610434
|
||||||
|
average cache miss response time (ms): 15.037237987495695
|
||||||
|
cache throughput (requests / s) : 71.68390271247678
|
||||||
|
real throughput (requests / s) : 62.85067778546538
|
||||||
|
|
||||||
|
--- weighted_friend_readonly 0.5 Results ---
|
||||||
|
hits: 1211 misses: 8789 ratio: 0.1211
|
||||||
|
average response time (ms) : 13.146774506568908
|
||||||
|
average cache hit response time (ms) : 0.001021990787874658
|
||||||
|
average cache miss response time (ms): 14.958073436664577
|
||||||
|
cache throughput (requests / s) : 76.06428477953591
|
||||||
|
real throughput (requests / s) : 66.63640929923034
|
||||||
|
|
||||||
|
--- weighted_friend_readonly 0.75 Results ---
|
||||||
|
hits: 2297 misses: 7703 ratio: 0.2297
|
||||||
|
average response time (ms) : 11.466057205200196
|
||||||
|
average cache hit response time (ms) : 0.0009958153451895475
|
||||||
|
average cache miss response time (ms): 14.884887013391413
|
||||||
|
cache throughput (requests / s) : 87.21393780823547
|
||||||
|
real throughput (requests / s) : 75.28993183954742
|
||||||
|
|
||||||
|
--- weighted_friend_readonly 1 Results ---
|
||||||
|
hits: 9999 misses: 1 ratio: 0.9999
|
||||||
|
average response time (ms) : 0.0020474910736083983
|
||||||
|
average cache hit response time (ms) : 0.0007939655812505079
|
||||||
|
average cache miss response time (ms): 12.536048889160156
|
||||||
|
cache throughput (requests / s) : 488402.6176669228
|
||||||
|
real throughput (requests / s) : 790.3142309599609
|
||||||
@@ -40,33 +40,33 @@ cache throughput (requests / s) : 107.27478709180934
|
|||||||
real throughput (requests / s) : 49.79781897651969
|
real throughput (requests / s) : 49.79781897651969
|
||||||
|
|
||||||
--- weighted_friend_readonly 0.25 Results ---
|
--- weighted_friend_readonly 0.25 Results ---
|
||||||
hits: 635 misses: 9365 ratio: 0.0635
|
hits: 2163 misses: 7837 ratio: 0.2163
|
||||||
average response time (ms) : 12.026190519332886
|
average response time (ms) : 10.187475109100342
|
||||||
average cache hit response time (ms) : 0.0014263813889871433
|
average cache hit response time (ms) : 0.08817761792003474
|
||||||
average cache miss response time (ms): 12.841537580474837
|
average cache miss response time (ms): 12.974865752640344
|
||||||
cache throughput (requests / s) : 83.15185082029382
|
cache throughput (requests / s) : 98.15974903405778
|
||||||
real throughput (requests / s) : 75.84771942953694
|
real throughput (requests / s) : 88.35527295489469
|
||||||
|
|
||||||
--- weighted_friend_readonly 0.50 Results ---
|
--- weighted_friend_readonly 0.50 Results ---
|
||||||
hits: 1072 misses: 8928 ratio: 0.1072
|
hits: 2766 misses: 7234 ratio: 0.2766
|
||||||
average response time (ms) : 11.465663266181945
|
average response time (ms) : 9.404127931594848
|
||||||
average cache hit response time (ms) : 0.001328649805552924
|
average cache hit response time (ms) : 0.07571936170863484
|
||||||
average cache miss response time (ms): 12.842205236248645
|
average cache miss response time (ms): 12.97094823907415
|
||||||
cache throughput (requests / s) : 87.21693431809628
|
cache throughput (requests / s) : 106.33628203209798
|
||||||
real throughput (requests / s) : 79.27228928100207
|
real throughput (requests / s) : 95.07378087157565
|
||||||
|
|
||||||
--- weighted_friend_readonly 0.75 Results ---
|
--- weighted_friend_readonly 0.75 Results ---
|
||||||
hits: 2253 misses: 7747 ratio: 0.2253
|
hits: 4087 misses: 5913 ratio: 0.4087
|
||||||
average response time (ms) : 9.959305834770202
|
average response time (ms) : 7.68894898891449
|
||||||
average cache hit response time (ms) : 0.0012218290786980208
|
average cache hit response time (ms) : 0.05545212465887814
|
||||||
average cache miss response time (ms): 12.855338268592709
|
average cache miss response time (ms): 12.965137333952995
|
||||||
cache throughput (requests / s) : 100.40860443393278
|
cache throughput (requests / s) : 130.05678688228338
|
||||||
real throughput (requests / s) : 90.2328882960655
|
real throughput (requests / s) : 114.06672701630119
|
||||||
|
|
||||||
--- weighted_friend_readonly 1 Results ---
|
--- weighted_friend_readonly 1 Results ---
|
||||||
hits: 9992 misses: 8 ratio: 0.9992
|
hits: 9992 misses: 8 ratio: 0.9992
|
||||||
average response time (ms) : 0.01112067699432373
|
average response time (ms) : 0.011892271041870118
|
||||||
average cache hit response time (ms) : 0.0008874601894039646
|
average cache hit response time (ms) : 0.0006238444506215324
|
||||||
average cache miss response time (ms): 12.792408466339111
|
average cache miss response time (ms): 14.086157083511353
|
||||||
cache throughput (requests / s) : 89922.582996559
|
cache throughput (requests / s) : 84088.22810035326
|
||||||
real throughput (requests / s) : 1052.9166753169109
|
real throughput (requests / s) : 1066.9048383247582
|
||||||
Reference in New Issue
Block a user