Skip to content

Commit

Permalink
pip update and remove playwright. also switch to non-async redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
milsman2 committed Apr 17, 2024
1 parent af0edf9 commit 8aa1e9e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
37 changes: 18 additions & 19 deletions scraper/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
aiosqlite==0.19.0
aiosqlite==0.20.0
alembic==1.13.1
annotated-types==0.6.0
anyio==4.2.0
astroid==3.0.3
anyio==4.3.0
astroid==3.1.0
asttokens==2.4.1
async-timeout==4.0.3
asyncpg==0.29.0
beautifulsoup4==4.12.3
black==24.2.0
black==24.4.0
certifi==2024.2.2
click==8.1.7
colorama==0.4.6
Expand All @@ -16,35 +16,34 @@ exceptiongroup==1.2.0
executing==2.0.1
greenlet==3.0.3
h11==0.14.0
httpcore==1.0.2
httpx==0.26.0
httpcore==1.0.5
httpx==0.27.0
icecream==2.1.3
idna==3.6
idna==3.7
iniconfig==2.0.0
isort==5.13.2
Mako==1.3.2
Mako==1.3.3
MarkupSafe==2.1.5
mccabe==0.7.0
mypy-extensions==1.0.0
packaging==23.2
packaging==24.0
pathspec==0.12.1
platformdirs==4.2.0
playwright==1.41.2
pluggy==1.4.0
psycopg2-binary==2.9.9
pydantic==2.6.1
pydantic==2.7.0
pydantic-settings==2.2.1
pydantic_core==2.16.2
pyee==11.0.1
pydantic_core==2.18.1
pyee==11.1.0
Pygments==2.17.2
pylint==3.0.3
pytest==8.0.0
pylint==3.1.0
pytest==8.1.1
python-dotenv==1.0.1
redis==5.0.3
six==1.16.0
sniffio==1.3.0
sniffio==1.3.1
soupsieve==2.5
SQLAlchemy==2.0.27
SQLAlchemy==2.0.29
tomli==2.0.1
tomlkit==0.12.3
typing_extensions==4.9.0
tomlkit==0.12.4
typing_extensions==4.11.0
37 changes: 21 additions & 16 deletions scraper/src/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import asyncio

import redis.asyncio as redis
import redis
from icecream import ic
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
Expand All @@ -31,6 +31,23 @@ 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):
ic()
if redis_client.exists(disc_golfer_id):
disc_golfer = redis_client.hgetall(str(disc_golfer_id))
return disc_golfer
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)
await engine.dispose()
return disc_golfer


async def run():
ic()
redis_client = redis.Redis(
Expand All @@ -39,22 +56,10 @@ async def run():
password=settings.REDIS_PW,
db=settings.REDIS_DB,
)
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_id = str(12626)
if await redis_client.exists(disc_golfer_id):
disc_golfer = await redis_client.hgetall(str(disc_golfer_id))
await engine.dispose()
return disc_golfer
else:
disc_golfer = await db_fetch(async_session)
if disc_golfer:
await redis_client.hset(disc_golfer_id, mapping=disc_golfer)
await engine.dispose()
return disc_golfer
await redis_client.aclose()
disc_golfer = await cache_fetch(redis_client, disc_golfer_id)
ic(disc_golfer)
redis_client.close()


if __name__ == "__main__":
Expand Down

0 comments on commit 8aa1e9e

Please sign in to comment.