1
0
mirror of https://github.com/ltcptgeneral/cs239-caching.git synced 2025-04-01 12:33:25 +00:00

add time stamp (in ms) to get response

This commit is contained in:
Arthur Lu 2025-02-17 22:04:23 +00:00 committed by root
parent ea88a0649c
commit 44dbf976b5

@ -4,6 +4,7 @@ from cache.cache import BaselineCache
from cache.prefetch_cache import PrefetchCache
from cache.tiered_cache import TieredCache
from config import CACHE_STRATEGY, CACHE_LIMIT, L2_CACHE_LIMIT
import time
app = FastAPI()
@ -20,16 +21,17 @@ else:
@app.get("/user/{user_id}")
def fetch_user_profile(user_id: str):
"""Fetch user profile with caching"""
start = time.time()
cached_profile = cache.get(user_id)
if cached_profile:
return {"user_id": user_id, "profile": cached_profile, "source": "cache"}
return {"user_id": user_id, "profile": cached_profile, "source": "cache", "time_ms": (time.time() - start) * 1000}
profile = get_user_profile(user_id)
if profile is None:
raise HTTPException(status_code=404, detail="User not found")
cache.put(user_id, profile) # Store in cache
return {"user_id": user_id, "profile": profile, "source": "database"}
return {"user_id": user_id, "profile": profile, "source": "database", "time_ms": (time.time() - start) * 1000}
@app.post("/update_user/")
def modify_user_profile(user_id: str, name: str, followers: int, bio: str, posts: str):