implement in memory cache

This commit is contained in:
agatha 2024-04-13 21:44:41 -04:00
parent 8563addf49
commit ca2c990002

View File

@ -9,6 +9,8 @@ UNSHORTEN = {
't.co': unshorten_twitter 't.co': unshorten_twitter
} }
CACHE = {}
app = FastAPI(docs_url=None, redoc_url=None) app = FastAPI(docs_url=None, redoc_url=None)
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
@ -29,4 +31,10 @@ async def receive_url(url: Optional[str] = None):
if domain not in UNSHORTEN: if domain not in UNSHORTEN:
return {"error": f"cannot unshorten {domain}"} 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