Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis Expire setup #81

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ async def get_meme_queue_length_by_key(key: str) -> int:
return await redis_client.scard(key)


async def add_memes_to_queue_by_key(key: str, memes: list[dict]) -> int:
async def add_memes_to_queue_by_key(
key: str, memes: list[dict],
expire: int = 3600
) -> None:
jsoned_memes = [orjson.dumps(meme) for meme in memes]
# TODO: add ttl, probably using redis transactions
return await redis_client.sadd(key, *jsoned_memes)
p = await redis_client.pipeline(transaction=True)
await p.sadd(key, *jsoned_memes)
await p.expire(key, expire)
await p.execute(raise_on_error=True)


def get_user_info_key(user_id: int) -> str:
Expand Down
6 changes: 4 additions & 2 deletions src/tgbot/senders/next_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ async def get_next_meme_for_user(user_id: int) -> MemeData | None:
while True:
meme = await meme_queue.get_next_meme_for_user(user_id)
if not meme: # no memes in queue
asyncio.create_task(meme_queue.check_queue(user_id))
return None
await meme_queue.generate_recommendations(user_id, limit=5)
meme = await meme_queue.get_next_meme_for_user(user_id)
if not meme:
return None

exists = await user_meme_reaction_exists(user_id, meme.id)
if not exists: # this meme wasn't sent yet
Expand Down
11 changes: 0 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import pytest_asyncio
from async_asgi_testclient import TestClient

from src.main import app


@pytest.fixture(autouse=True, scope="session")
def run_migrations() -> None:
Expand All @@ -23,12 +21,3 @@ def event_loop() -> Generator[asyncio.AbstractEventLoop, None, None]:
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()


@pytest_asyncio.fixture
async def client() -> AsyncGenerator[TestClient, None]:
host, port = "127.0.0.1", "9000"
scope = {"client": (host, port)}

async with TestClient(app, scope=scope) as client:
yield client
29 changes: 29 additions & 0 deletions tests/test_redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import datetime

import asyncio

import pytest
import pytest_asyncio

from src import redis


@pytest.mark.asyncio
async def test_add_memes_to_queue_by_key_ok():
user_id = 999
queue_key = redis.get_meme_queue_key(user_id)

memes = [
{'id': 1, 'recommended_by': 'best_algo'},
{'id': 2, 'recommended_by': 'best_algo'},
]

await redis.add_memes_to_queue_by_key(queue_key, memes, expire=1)

stored = await redis.redis_client.smembers(queue_key)
assert len(stored) == 2

await asyncio.sleep(3)

stored = await redis.redis_client.smembers(queue_key)
assert len(stored) == 0
Loading