feat: 쇼츠 목록을 요청 때가 아니라 워커가 하루 3번(07·12·18시 10분)과 켤 때 받아 둠, 캐시는 2일 — 사용자는 유튜브 응답을 기다리지 않음
This commit is contained in:
1 parent
e0a723a3e4
commit
ba70fbcd49
4 files changed
+39
-4
No files matched your search
@@ -11,13 +11,14 @@ from config.settings import settings
|
||||
from services.external import youtube
|
||||
|
||||
CACHE_KEY = "shorts:list"
|
||||
CACHE_SEC = 3600
|
||||
# 워커가 하루 3번(07·12·18시 10분) 새로 받는다. 한 번 실패해도 다음 회차까지 이전 목록을 쓰게 넉넉히 둔다
|
||||
CACHE_SEC = 2 * 86400
|
||||
MAX_ITEMS = 10
|
||||
|
||||
|
||||
class ShortsService:
|
||||
async def items(self) -> tuple[ErrorType, list[dict]]:
|
||||
"""오늘 화면 쇼츠 줄 [{id, title}]. API 키가 있으면 채널에서, 없으면 설정의 영상 ID 목록으로."""
|
||||
"""오늘 화면 쇼츠 줄 [{id, title}]. 워커가 받아 둔 목록을 읽고, 없을 때만(처음 켰을 때) 바로 받는다."""
|
||||
try:
|
||||
cached = await REDIS.conn.get(CACHE_KEY)
|
||||
except RedisError as e:
|
||||
@@ -25,6 +26,10 @@ class ShortsService:
|
||||
cached = None
|
||||
if cached:
|
||||
return ErrorType.SUCCESS, json.loads(cached)
|
||||
return await self.refresh()
|
||||
|
||||
async def refresh(self) -> tuple[ErrorType, list[dict]]:
|
||||
"""유튜브에서 새로 받아 캐시에 둔다. API 키가 있으면 채널에서, 없으면 설정의 영상 ID 목록으로."""
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
items = await self._fetch(client)
|
||||
|
||||
Reference in new issue
Block a user