clean up cache usage

This commit is contained in:
jetli 2024-12-26 08:48:34 +00:00
parent 550c49f3ec
commit 04b1d688d5
2 changed files with 0 additions and 68 deletions

View File

@ -3,7 +3,6 @@ from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from webapi.providers import common
from webapi.providers import cache # Import the new cache provider
from webapi.providers import logger
from webapi.providers import router
from webapi.providers import database
@ -19,7 +18,6 @@ def create_app() -> FastAPI:
register(app, exception_handler)
register(app, database)
register(app, cache)
register(app, logger)
register(app, router)
register(app, scheduler)

View File

@ -1,66 +0,0 @@
import redis.asyncio as redis
from redis.asyncio import Redis
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
# Declare redis_instance globally
redis_instance = None
def register(app):
@app.on_event("startup")
async def init_cache():
redis_instance = redis.Redis(
host="localhost", # Replace with your Redis host
port=6379, # Replace with your Redis port
db=0, # Replace with your Redis database number
decode_responses=True, # Optional: Enable decoded responses
)
# Test the Redis connectionafrom redis.asyncio import Redis
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
def register(app):
@app.on_event("startup")
async def init_cache():
# Connect to Redis running in Docker
redis_instance = Redis(
host="localhost", # If Redis is running on the same machine
port=6379, # Port mapped to Redis container
db=0,
decode_responses=True,
)
# Test Redis connection
try:
await redis_instance.ping()
print("Connected to Redis Docker container!")
except Exception as e:
print(f"Failed to connect to Redis: {e}")
raise
# Initialize FastAPICache
FastAPICache.init(RedisBackend(redis_instance), prefix="fastapi-cache")
@app.on_event("shutdown")
async def shutdown_redis():
await redis_instance.close()
try:
await redis_instance.ping()
print("Redis connection established successfully!")
except Exception as e:
print(f"Failed to connect to Redis: {e}")
raise
# Initialize FastAPICache
FastAPICache.init(RedisBackend(redis_instance), prefix="fastapi-cache")
@app.on_event("shutdown")
async def close_redis():
if redis_instance:
await redis_instance.close()
print("Redis connection closed gracefully.")