From 72b72a949fdcbfc631006c1cf8463acb91ce8392 Mon Sep 17 00:00:00 2001 From: Derek Wang <derek.kiah.wang@gmail.com> Date: Sun, 2 Mar 2025 19:41:39 -0800 Subject: [PATCH] Added ReadAfterWrite --- app/cache/read_after_write_cache.py | 20 ++++++++++++++++++++ app/config_readafterwrite.yaml | 4 ++++ app/main.py | 4 ++++ 3 files changed, 28 insertions(+) create mode 100644 app/cache/read_after_write_cache.py create mode 100644 app/config_readafterwrite.yaml diff --git a/app/cache/read_after_write_cache.py b/app/cache/read_after_write_cache.py new file mode 100644 index 0000000..002626d --- /dev/null +++ b/app/cache/read_after_write_cache.py @@ -0,0 +1,20 @@ +from .cache import BaselineCache +from database import get_user_profile + +class ReadAfterWriteCache(BaselineCache): + + def __init__(self, limit): + super().__init__( limit ) + + def invalidate(self, key: str) -> bool: + # basic delete invalidation, but after writing, we immediately read the value and add it to the cache + invalidated = False + if key in self.cache: + del self.cache[key] + invalidated = True + + newData = get_user_profile( key ) + self.put( key, newData ) + + return invalidated + \ No newline at end of file diff --git a/app/config_readafterwrite.yaml b/app/config_readafterwrite.yaml new file mode 100644 index 0000000..3ab976c --- /dev/null +++ b/app/config_readafterwrite.yaml @@ -0,0 +1,4 @@ +cache_strategy: "ReadAfterWrite" +cache_limit: 50 +l2_cache_limit: 100 # unused +db_file: "llmData_sns.json" # Change this to the name of any json file within the "database/datastore" folder \ No newline at end of file diff --git a/app/main.py b/app/main.py index 470c48e..18c7896 100644 --- a/app/main.py +++ b/app/main.py @@ -6,6 +6,7 @@ from cache.tiered_cache import TieredCache from cache.eviction_seive import SeiveCache from cache.nocache import NoCache from cache.idealcache import IdealCache +from cache.read_after_write_cache import ReadAfterWriteCache from config import CACHE_STRATEGY, CACHE_LIMIT, L2_CACHE_LIMIT import time @@ -30,6 +31,9 @@ elif CACHE_STRATEGY == "None": elif CACHE_STRATEGY == "Ideal": print("Using ideal cache strategy") cache = IdealCache(limit=CACHE_LIMIT) +elif CACHE_STRATEGY == "ReadAfterWrite": + print("Using read-after-write cache strategy") + cache = ReadAfterWriteCache(limit=CACHE_LIMIT) else: raise ValueError(f"Invalid CACHE_STRATEGY: {CACHE_STRATEGY}")