mirror of
https://github.com/ltcptgeneral/cs239-caching.git
synced 2025-03-30 03:35:18 +00:00
Added ReadAfterWrite
This commit is contained in:
parent
35ea5a234f
commit
72b72a949f
20
app/cache/read_after_write_cache.py
vendored
Normal file
20
app/cache/read_after_write_cache.py
vendored
Normal file
@ -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
|
||||
|
4
app/config_readafterwrite.yaml
Normal file
4
app/config_readafterwrite.yaml
Normal file
@ -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
|
@ -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}")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user