Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/api/app/core/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ async def clear_pattern(self, pattern: str) -> int:
logger.error(f"Cache clear pattern error for {pattern}: {e}")
return 0

async def flush_all(self) -> bool:
"""Flush all keys from the current database"""
try:
await self.redis.flushdb()
logger.info("Cache flushed successfully")
return True
except redis.RedisError as e:
logger.error(f"Cache flush error: {e}")
return False

async def get_stats(self) -> Dict[str, Any]:
"""Get cache statistics"""
try:
Expand Down
19 changes: 19 additions & 0 deletions backend/api/app/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import logging
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from app.api.v1 import cache, health, ncbi_links, version
from app.core.config import get_settings
from app.core.dependencies import get_cache_service

logger = logging.getLogger(__name__)
settings = get_settings()


@asynccontextmanager
async def lifespan(app: FastAPI):
"""Startup and shutdown events for the application"""
# Startup: flush cache to ensure fresh data after restart
cache_service = await get_cache_service()
await cache_service.flush_all()
logger.info("Cache cleared on startup")
yield
# Shutdown: close cache connection
await cache_service.close()


app = FastAPI(
title="BRC Analytics API",
version=settings.APP_VERSION,
docs_url="/api/docs",
redoc_url="/api/redoc",
lifespan=lifespan,
)

app.add_middleware(
Expand Down
Loading