Skip to content

Commit

Permalink
testing redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
milsman2 committed Apr 22, 2024
1 parent f9b8f5f commit 7a7fe33
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
4 changes: 4 additions & 0 deletions scraper/src/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .redis import redis_client
from .postgresql_conn import async_session, engine

__all__ = ["redis_client", "async_session", "engine"]
11 changes: 11 additions & 0 deletions scraper/src/db/postgresql_conn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Create a connection to the PostgreSQL database using SQLAlchemy's asyncpg driver.
"""

from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from scraper.src.config import settings

engine = create_async_engine(
f"postgresql+asyncpg://{settings.PG_USER}:{settings.PG_PW}@{settings.PG_HOST}/{settings.PG_DB}"
)
async_session = async_sessionmaker(engine, expire_on_commit=False)
13 changes: 13 additions & 0 deletions scraper/src/db/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Create a Redis client to interact with the Redis server.
"""

import redis
from scraper.src.config import settings

redis_client = redis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
password=settings.REDIS_PW,
db=settings.REDIS_DB,
)
27 changes: 8 additions & 19 deletions scraper/src/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import redis
from icecream import ic
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from scraper.src.config import settings
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from scraper.src.db import redis_client, async_session, engine
from scraper.src.models import DiscGolfer


async def db_fetch(async_session: async_sessionmaker[AsyncSession]):
async def db_fetch(a_session: async_sessionmaker[AsyncSession]):
ic()
async with async_session() as session:
async with a_session() as session:
stmt = select(DiscGolfer).order_by(DiscGolfer.id)
result = await session.execute(stmt)
disc_golfer = result.scalars().first()
Expand All @@ -31,31 +31,20 @@ async def db_fetch(async_session: async_sessionmaker[AsyncSession]):
return disc_golfer_obj


async def cache_fetch(redis_client: redis.Redis, disc_golfer_id: str):
async def cache_fetch(cache_client: redis.Redis, disc_golfer_id: str):
ic()
if redis_client.exists(disc_golfer_id):
disc_golfer = redis_client.hgetall(str(disc_golfer_id))
return disc_golfer
if cache_client.exists(disc_golfer_id):
return cache_client.hgetall(str(disc_golfer_id))
else:
engine = create_async_engine(
f"postgresql+asyncpg://{settings.PG_USER}:{settings.PG_PW}@{settings.PG_HOST}/{settings.PG_DB}"
)
async_session = async_sessionmaker(engine, expire_on_commit=False)
disc_golfer = await db_fetch(async_session)
if disc_golfer:
redis_client.hset(disc_golfer_id, mapping=disc_golfer)
cache_client.hset(disc_golfer_id, mapping=disc_golfer)
await engine.dispose()
return disc_golfer


async def run():
ic()
redis_client = redis.Redis(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
password=settings.REDIS_PW,
db=settings.REDIS_DB,
)
disc_golfer_id = str(12626)
disc_golfer = await cache_fetch(redis_client, disc_golfer_id)
ic(disc_golfer)
Expand Down

0 comments on commit 7a7fe33

Please sign in to comment.