Skip to content

Commit

Permalink
add cache to router
Browse files Browse the repository at this point in the history
  • Loading branch information
k4sud0n committed Dec 24, 2024
1 parent fac78c0 commit b248ce9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 53 deletions.
39 changes: 22 additions & 17 deletions menu/router.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
from datetime import datetime
from datetime import datetime, timedelta

from fastapi import APIRouter
from models import Menu

router = APIRouter()

menu_cache = {}
cache_expiry_time = None


@router.get("/menu/{location}")
@router.post("/menu/{location}")
async def read_menu(location: str):
menu = await Menu.filter(location=location, date=datetime.today().date()).first()
if not menu:
return {"error": "Menu not found"}
global cache_expiry_time

items = [
{"description": getattr(menu, f"menu{i}", None)}
for i in range(1, 6)
if getattr(menu, f"menu{i}", None)
]
now = datetime.now()
today = now.date()

return {
"version": "2.0",
"template": {"outputs": [{"carousel": {"type": "textCard", "items": items}}]},
}
if not cache_expiry_time or now >= cache_expiry_time:
menu_cache.clear()
cache_expiry_time = datetime.combine(
today + timedelta(days=1), datetime.min.time()
)

cache_key = f"{location}_{today}"
if cache_key in menu_cache:
return menu_cache[cache_key]

@router.post("/menu/{location}")
async def read_menu(location: str):
menu = await Menu.filter(location=location, date=datetime.today().date()).first()
# 캐시가 없으면 DB에서 읽기
menu = await Menu.filter(location=location, date=today).first()
if not menu:
return {"error": "Menu not found"}

Expand All @@ -36,7 +38,10 @@ async def read_menu(location: str):
if getattr(menu, f"menu{i}", None)
]

return {
response = {
"version": "2.0",
"template": {"outputs": [{"carousel": {"type": "textCard", "items": items}}]},
}

menu_cache[cache_key] = response
return response
55 changes: 19 additions & 36 deletions notice/router.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,34 @@
from datetime import datetime, timedelta

from fastapi import APIRouter

from models import Notice

router = APIRouter()

notice_cache = {}
cache_expiry_time = None


@router.get("/notice")
@router.post("/notice")
async def read_notice(limit: int = 5):
notices = await Notice.all().order_by("-date")[:limit]
if not notices:
return {"error": "Notice not found"}
global cache_expiry_time

items = [
{
"title": notice.title,
"description": notice.date,
"imageUrl": "https://t1.kakaocdn.net/openbuilder/sample/img_002.jpg",
"link": {"web": notice.link},
}
for notice in notices
]
now = datetime.now()
today = now.date()

response = {
"version": "2.0",
"template": {
"outputs": [
{
"listCard": {
"header": {"title": "한국외대 공지사항"},
"items": items,
"buttons": [
{
"action": "webLink",
"label": "더 보기",
"webLinkUrl": "https://www.hufs.ac.kr/hufs/11281/subview.do",
},
],
}
}
]
},
}

return response
if not cache_expiry_time or now >= cache_expiry_time:
notice_cache.clear()
cache_expiry_time = datetime.combine(
today + timedelta(days=1), datetime.min.time()
)

cache_key = f"{today}"
if cache_key in notice_cache:
return notice_cache[cache_key]

@router.post("/notice")
async def read_notice(limit: int = 5):
# 캐시가 없으면 DB에서 읽기
notices = await Notice.all().order_by("-date")[:limit]
if not notices:
return {"error": "Notice not found"}
Expand Down Expand Up @@ -82,4 +64,5 @@ async def read_notice(limit: int = 5):
},
}

notice_cache[cache_key] = response
return response

0 comments on commit b248ce9

Please sign in to comment.