Skip to content

Commit

Permalink
Merge pull request #392 from LACMTA:2023-api-optimization
Browse files Browse the repository at this point in the history
Fix to use `aioredis.from_url` instead of connection pool
  • Loading branch information
albertkun authored Nov 17, 2023
2 parents a163231 + 1492546 commit d37d593
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

# from redis import asyncio as aioredis
from enum import Enum
from aioredis import create_redis_pool


# for OAuth2
Expand Down Expand Up @@ -87,7 +86,7 @@ async def initialize_redis():
logging.info(f"Connecting to Redis at {Config.REDIS_URL}")
for i in range(5): # Retry up to 5 times
try:
redis = await create_redis_pool(Config.REDIS_URL)
redis = await aioredis.from_url(Config.REDIS_URL)
break # If the connection is successful, break out of the loop
except aioredis.exceptions.ConnectionError as e:
logging.error(f"Failed to connect to Redis: {e}")
Expand Down Expand Up @@ -739,7 +738,7 @@ async def get_all_routes():

@app.on_event("startup")
async def startup_event():
redis_pool = await create_redis_pool(Config.REDIS_URL)
redis_pool = await aioredis.from_url(Config.REDIS_URL)
redis = RedisBackend(redis_pool)
FastAPICache.init(backend=redis, prefix="fastapi-cache")
uvicorn_access_logger = logging.getLogger("uvicorn.access")
Expand Down

0 comments on commit d37d593

Please sign in to comment.