mirror of
https://github.com/ltcptgeneral/cs239-caching.git
synced 2025-09-08 00:07:22 +00:00
prefetch test
This commit is contained in:
17
app/cache/prefetch_cache.py
vendored
17
app/cache/prefetch_cache.py
vendored
@@ -1,5 +1,5 @@
|
||||
from .cache import Cache
|
||||
from database import get_user_profile
|
||||
from database import get_user_profile, get_friends
|
||||
from collections import OrderedDict
|
||||
import math
|
||||
|
||||
@@ -39,15 +39,12 @@ class PrefetchCache(Cache):
|
||||
|
||||
def prefetch(self, profile) -> bool:
|
||||
evict = False
|
||||
for i in range(math.ceil(self.limit*0.1)):
|
||||
if i < len(profile["friends"]):
|
||||
data = get_user_profile(profile["friends"][i])
|
||||
if len(self.cache) >= self.limit:
|
||||
self.cache.popitem(last = False)
|
||||
evict = True
|
||||
self.cache[profile["friends"][i]] = data
|
||||
else:
|
||||
break
|
||||
friends_prof = get_friends(profile["user_id"], math.ceil(self.limit*0.1))
|
||||
for i in friends_prof:
|
||||
if len(self.cache) >= self.limit:
|
||||
self.cache.popitem(last = False)
|
||||
evict = True
|
||||
self.cache[i] = friends_prof[i]
|
||||
return evict
|
||||
|
||||
def invalidate(self, key: str) -> bool:
|
||||
|
@@ -1,4 +1,4 @@
|
||||
cache_strategy: "Tiered" # Change this to "Prefetch" or "Tiered" or "Seive"
|
||||
cache_strategy: "Prefetch" # Change this to "Prefetch" or "Tiered" or "Seive"
|
||||
cache_limit: 10
|
||||
l2_cache_limit: 100
|
||||
db_file: "llmData_sns.json" # Change this to the name of any json file within the "database/datastore" folder
|
@@ -1,6 +1,7 @@
|
||||
|
||||
from tinydb import TinyDB, Query
|
||||
from config import DB_FILE
|
||||
import random
|
||||
|
||||
DB_LOCATION = "database/datastore/" + DB_FILE
|
||||
|
||||
@@ -11,6 +12,22 @@ User = Query()
|
||||
def get_user_ids():
|
||||
return [x["user_id"] for x in db.all()]
|
||||
|
||||
def get_user_friend_ids():
|
||||
user_friends = {}
|
||||
for x in db.all():
|
||||
user_friends[x["user_id"]] = x["friends"]
|
||||
return user_friends
|
||||
|
||||
def get_friends(user_id, num_friends):
|
||||
friends = {}
|
||||
curr_user = db.search(User.user_id == user_id)
|
||||
random.seed(0)
|
||||
if not curr_user:
|
||||
return {}
|
||||
for f in random.sample(curr_user[0]["friends"], num_friends):
|
||||
friends[f] = db.search(User.user_id == user_id)[0]
|
||||
return friends
|
||||
|
||||
def get_user_profile(user_id):
|
||||
"""Fetch user profile from TinyDB"""
|
||||
result = db.search(User.user_id == user_id)
|
||||
|
@@ -1,5 +1,5 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from database import get_user_ids, get_user_profile, update_user_profile
|
||||
from database import get_user_ids, get_user_profile, update_user_profile, get_user_friend_ids
|
||||
from cache.cache import BaselineCache
|
||||
from cache.prefetch_cache import PrefetchCache
|
||||
from cache.tiered_cache import TieredCache
|
||||
@@ -25,6 +25,10 @@ else:
|
||||
def fetch_user_ids():
|
||||
return {"ids": get_user_ids()}
|
||||
|
||||
@app.get("/users_and_friends")
|
||||
def fetch_user_and_friends():
|
||||
return get_user_friend_ids()
|
||||
|
||||
@app.get("/user/{user_id}")
|
||||
def fetch_user_profile(user_id: str):
|
||||
"""Fetch user profile with caching"""
|
||||
|
Reference in New Issue
Block a user