mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-01-17 11:40:39 +03:00
wip: cache
This commit is contained in:
@@ -3,6 +3,8 @@ from typing import Annotated
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from fastapi_cache.decorator import cache
|
||||
|
||||
from app.database import get_db
|
||||
from app.models.shakespeare import Paragraph
|
||||
|
||||
@@ -12,6 +14,7 @@ router = APIRouter(prefix="/v1/shakespeare")
|
||||
@router.get(
|
||||
"/",
|
||||
)
|
||||
@cache(namespace="test-2", expire=60)
|
||||
async def find_paragraph(
|
||||
character: Annotated[str, Query(description="Character name")],
|
||||
db_session: AsyncSession = Depends(get_db),
|
||||
|
||||
13
app/main.py
13
app/main.py
@@ -1,6 +1,8 @@
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Depends
|
||||
from fastapi_cache import FastAPICache
|
||||
from fastapi_cache.backends.redis import RedisBackend
|
||||
|
||||
from app.api.nonsense import router as nonsense_router
|
||||
from app.api.shakespeare import router as shakespeare_router
|
||||
@@ -8,7 +10,7 @@ from app.api.stuff import router as stuff_router
|
||||
from app.utils.logging import AppLogger
|
||||
from app.api.user import router as user_router
|
||||
from app.api.health import router as health_router
|
||||
from app.redis import get_redis
|
||||
from app.redis import get_redis, get_cache
|
||||
from app.services.auth import AuthBearer
|
||||
|
||||
logger = AppLogger().get_logger()
|
||||
@@ -18,11 +20,18 @@ logger = AppLogger().get_logger()
|
||||
async def lifespan(app: FastAPI):
|
||||
# Load the redis connection
|
||||
app.state.redis = await get_redis()
|
||||
|
||||
|
||||
|
||||
try:
|
||||
# Initialize the cache with the redis connection
|
||||
redis_cache = await get_cache()
|
||||
FastAPICache.init(RedisBackend(redis_cache), prefix="fastapi-cache")
|
||||
logger.info(FastAPICache.get_cache_status_header())
|
||||
yield
|
||||
finally:
|
||||
# close redis connection and release the resources
|
||||
app.state.redis.close()
|
||||
await app.state.redis.close()
|
||||
|
||||
|
||||
app = FastAPI(title="Stuff And Nonsense API", version="0.6", lifespan=lifespan)
|
||||
|
||||
@@ -9,3 +9,10 @@ async def get_redis():
|
||||
encoding="utf-8",
|
||||
decode_responses=True,
|
||||
)
|
||||
|
||||
|
||||
async def get_cache():
|
||||
return await redis.from_url(
|
||||
global_settings.redis_url.unicode_string(),
|
||||
decode_responses=False,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user