From ca2c990002d581cd95b913e0e01cd0c103e1b58c Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 13 Apr 2024 21:44:41 -0400 Subject: [PATCH] implement in memory cache --- server/src/main.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/src/main.py b/server/src/main.py index 32bf17e..8b65d11 100644 --- a/server/src/main.py +++ b/server/src/main.py @@ -9,6 +9,8 @@ UNSHORTEN = { 't.co': unshorten_twitter } +CACHE = {} + app = FastAPI(docs_url=None, redoc_url=None) app.add_middleware( CORSMiddleware, @@ -29,4 +31,10 @@ async def receive_url(url: Optional[str] = None): if domain not in UNSHORTEN: return {"error": f"cannot unshorten {domain}"} - return UNSHORTEN[domain](url) + if url in CACHE: + unshortened = CACHE[url] + else: + unshortened = UNSHORTEN[domain](url) + CACHE[url] = unshortened + + return unshortened