Last active
September 6, 2026 11:00
-
-
Save AbhinavOmprakash/412665cd3e3cf98cb44e0e7111f3010e to your computer and use it in GitHub Desktop.
Adaptive Cache replacement policy implementation in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| @dataclass | |
| class Item: | |
| id: int | |
| value: any | |
| class ARCPolicy: | |
| def __init__(self, cache_size, lru_1_target_size): | |
| self.lru_1 = [] | |
| self.lru_2 = [] | |
| self.ghost_lru_1 = [] | |
| self.ghost_lru_2 = [] | |
| self.cache_size = cache_size | |
| self.lru_1_target_size = lru_1_target_size | |
| def register_access(self, item, idx): | |
| # case 1 where idx is not in any list | |
| if idx not in self.lru_1 and idx not in self.lru_2: | |
| if item.id not in self.ghost_lru_1 and item.id not in self.ghost_lru_2: # new | |
| self.lru_1.append(idx) # new | |
| else: | |
| ghost_lru_1_size = len(self.ghost_lru_1) | |
| ghost_lru_2_size = len(self.ghost_lru_2) | |
| if item.id in self.ghost_lru_1: | |
| old_target_size = self.lru_1_target_size | |
| if ghost_lru_1_size >= ghost_lru_2_size: | |
| self.lru_1_target_size = min(1+ self.lru_1_target_size, self.cache_size) | |
| else: | |
| self.lru_1_target_size = min(1+ (ghost_lru_2_size//ghost_lru_1_size), self.cache_size) | |
| print(f"Target size changed from {old_target_size} to {self.lru_1_target_size}") | |
| self.ghost_lru_1.remove(item.id) | |
| self.lru_2.append(idx) | |
| else: | |
| old_target_size = self.lru_1_target_size | |
| if ghost_lru_2_size >= ghost_lru_1_size: | |
| self.lru_1_target_size = max(self.lru_1_target_size-1, 0) | |
| else: | |
| self.lru_1_target_size = max((self.lru_1_target_size - (ghost_lru_1_size//ghost_lru_2_size)), 0) | |
| print(f"Target size changed from {old_target_size} to {self.lru_1_target_size}") | |
| self.ghost_lru_2.remove(item.id) | |
| self.lru_2.append(idx) | |
| elif idx in self.lru_1: | |
| self.lru_2.append(idx) | |
| self.lru_1.remove(idx) | |
| # case 3 where idx is in lru_1 | |
| elif idx in self.lru_2: | |
| # remove and put idx to the end of the list | |
| self.lru_2.remove(idx) | |
| self.lru_2.append(idx) | |
| def evict_ghost_cache(self): | |
| ghost_lru_1_size = len(self.ghost_lru_1) | |
| ghost_lru_2_size = len(self.ghost_lru_2) | |
| lru_1_size = len(self.lru_1) | |
| lru_2_size = len(self.lru_2) | |
| if ghost_lru_1_size + ghost_lru_2_size == self.cache_size: | |
| self.ghost_lru_1.pop(0) | |
| elif ghost_lru_1_size + ghost_lru_2_size + lru_2_size + lru_1_size == 2*self.cache_size: | |
| self.ghost_lru_2.pop(0) | |
| def evict(self, cache): | |
| self.evict_ghost_cache() # new | |
| if len(self.lru_1) > 0 and len(self.lru_1) >= self.lru_1_target_size: | |
| evict_idx = self.lru_1.pop(0) | |
| evicted_item = cache[evict_idx] # new | |
| ## TODO add to blogpost | |
| if len(self.ghost_lru_1) < len(self.lru_1): | |
| self.ghost_lru_1.append(evicted_item.id) # new | |
| else: | |
| if len(self.ghost_lru_1) > 0: | |
| self.ghost_lru_1.pop(0) | |
| self.ghost_lru_1.append(evicted_item.id) # new | |
| return evict_idx | |
| # case 2 | |
| else: | |
| assert len(self.lru_2) > 0 | |
| evict_idx = self.lru_2.pop(0) | |
| evicted_item = cache[evict_idx] # new | |
| if len(self.ghost_lru_2) < len(self.lru_2): | |
| self.ghost_lru_2.append(evicted_item.id) # new | |
| else: | |
| self.ghost_lru_2.pop(0) | |
| self.ghost_lru_2.append(evicted_item.id) # new | |
| return evict_idx | |
| class CacheManager: | |
| def __init__(self, cache_size): | |
| self.cache_size = cache_size; | |
| self.cache = [None for _ in range(cache_size)] | |
| self.item_id_to_cache_index = {} | |
| self.empty_idx = 0; | |
| # new | |
| self.arc_policy = ARCPolicy(cache_size, cache_size//2) | |
| def access(self, item_id): | |
| if item_id in self.item_id_to_cache_index: | |
| item = self.cache[self.item_id_to_cache_index[item_id]] # new | |
| self.arc_policy.register_access(item, self.item_id_to_cache_index[item_id]) # new | |
| return item | |
| else: | |
| item = fetch_from_disk(item_id) | |
| self.insert(item) | |
| return item | |
| def insert(self, item): | |
| # case when cache has space and we don't have to evict | |
| if self.empty_idx < self.cache_size: | |
| self.cache[self.empty_idx] = item | |
| self.arc_policy.register_access(item,self.empty_idx) | |
| # Update item_id_to_cache_index | |
| self.item_id_to_cache_index[item.id] = self.empty_idx | |
| self.empty_idx +=1 | |
| else: | |
| # cache full, evict | |
| evict_idx = self.arc_policy.evict(self.cache) | |
| old_item = self.cache[evict_idx] | |
| # remove old item from item_id_to_cache_index | |
| self.item_id_to_cache_index.pop(old_item.id) | |
| # replace old item with new item | |
| self.cache[evict_idx] = item | |
| self.arc_policy.register_access(item, evict_idx) | |
| self.item_id_to_cache_index[item.id] = evict_idx | |
| def fetch_from_disk(id): | |
| # print(f"fetched item with id {id} from disk") | |
| return Item(id, "A value") | |
| def write_to_disk(item): | |
| # print(f"Written item with id {id} to disk") | |
| return item | |
| # play around with this | |
| CACHE_SIZE = 10 | |
| ss1 = list(range(1,100)) | |
| ss2= list(range(2,200)) | |
| ss3= list(range(3,200)) | |
| bp1 = list(range(301, 301+CACHE_SIZE)) + [301, 301, 303, 304, 305, 306, 307] | |
| bp2 = list(range(401, 401+CACHE_SIZE)) + [401, 402, 403, 404, 405, 406, 407] | |
| all_workloads = [ | |
| # seq scans | |
| ss1 | |
| , ss2 | |
| , ss3 | |
| , ss1 | |
| , ss2 | |
| , ss3 | |
| , bp1 | |
| , ss1 | |
| , ss2 | |
| , ss3 | |
| # batch processing workload | |
| , bp1 | |
| , bp1 | |
| , ss1 | |
| , bp2 | |
| , bp2 | |
| , ss1 | |
| , bp1 | |
| , bp1 | |
| # seq scans | |
| , ss1 | |
| , ss2 | |
| , ss3 | |
| , bp1 | |
| , ss1 | |
| , ss2 | |
| , ss3 | |
| ] | |
| cm=CacheManager(CACHE_SIZE) | |
| for wk in all_workloads: | |
| for item in wk: | |
| cm.access(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment