feat: 날씨각 백엔드 API (날씨·노을·지수·주말·계절·피드·로그인·반응)
- FastAPI + PostgreSQL + Redis, web·worker 분리, compose 에 backend·worker·postgres·redis 추가 - 기상청 단기·초단기·중기·특보, 에어코리아, Open-Meteo, TourAPI, 특일정보 연동 (첫 조회 때 받고 캐시) - 매일 03:00 worker 가 전국 공통 피드 칸을 정하고 지난 데이터를 정리 - 시드: 시군구 256곳, 지수 23개, 노을 대표 지점·명소, 데이트 명소, 읽을거리 - pytest 66개 (테스트 전용 DB·Redis 사용) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9cdc0d8fea
commit
e134295ce5
135 files changed
+9123
No files matched your search
Whitespace-only changes.
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncIterator
|
||||
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
from common.cache.redis_client import REDIS
|
||||
from common.logger import LOG
|
||||
|
||||
_RELEASE = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def redis_lock(key: str, ttl_sec: int = 10) -> AsyncIterator[bool]:
|
||||
"""잡았으면 True. 다른 요청이 잡고 있으면 False (기다리지 않음)."""
|
||||
token = uuid.uuid4().hex
|
||||
try:
|
||||
acquired = bool(await REDIS.conn.set(key, token, nx=True, ex=ttl_sec))
|
||||
except RedisError as e:
|
||||
LOG.error(f"[lock] {key} acquire failed: {e}")
|
||||
acquired = True
|
||||
try:
|
||||
yield acquired
|
||||
finally:
|
||||
if acquired:
|
||||
try:
|
||||
await REDIS.conn.eval(_RELEASE, 1, key, token)
|
||||
except RedisError as e:
|
||||
LOG.error(f"[lock] {key} release failed: {e}")
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
from typing import Optional
|
||||
|
||||
from redis.asyncio import Redis
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
from common.logger import LOG
|
||||
from config.settings import settings
|
||||
|
||||
|
||||
class RedisClient:
|
||||
def __init__(self) -> None:
|
||||
self._redis: Optional[Redis] = None
|
||||
|
||||
@property
|
||||
def conn(self) -> Redis:
|
||||
if self._redis is None:
|
||||
self._redis = Redis(host=settings.redis_host, port=settings.redis_port, db=settings.redis_db, decode_responses=True)
|
||||
return self._redis
|
||||
|
||||
async def stop(self) -> None:
|
||||
if self._redis is not None:
|
||||
await self._redis.aclose()
|
||||
self._redis = None
|
||||
|
||||
async def ping(self) -> bool:
|
||||
try:
|
||||
return bool(await self.conn.ping())
|
||||
except RedisError as e:
|
||||
LOG.error(f"[redis] ping failed: {e}")
|
||||
return False
|
||||
|
||||
|
||||
REDIS = RedisClient()
|
||||
Reference in new issue
Block a user