Skip to content

Commit

Permalink
[waicolle] use current transaction in get_price
Browse files Browse the repository at this point in the history
adds a tx argument to the “redis” methods so we can pass the current
transaction (or nothing and then it should work as usual)
  • Loading branch information
odrling committed Jan 19, 2025
1 parent ed40790 commit 02cac41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
37 changes: 29 additions & 8 deletions nanapi/utils/redis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Generic, TypeVar

import orjson
from edgedb import AsyncIOExecutor

from nanapi.database.redis.data_delete_by_key import data_delete_by_key
from nanapi.database.redis.data_get_by_key import DataGetByKeyResult, data_get_by_key
Expand All @@ -27,30 +28,50 @@ class BaseRedis(ABC, Generic[T]):
def __init__(self, key: str, global_key: bool = False):
self.key = key if global_key else make_redis_key(key)

async def get(self, sub_key: str | int | None = None) -> T | None:
async def get(
self, sub_key: str | int | None = None, tx: AsyncIOExecutor | None = None
) -> T | None:
key = self.key if sub_key is None else f'{self.key}:{sub_key}'
try:
resp = await data_get_by_key(get_edgedb(), key=key)
if tx is None:
tx = get_edgedb()

resp = await data_get_by_key(tx, key=key)
return self._decode(resp)
except Exception as e:
logger.exception(e)
return

async def set(self, value: T, sub_key: str | int | None = None, **kwargs):
async def set(
self,
value: T,
sub_key: str | int | None = None,
tx: AsyncIOExecutor | None = None,
):
key = self.key if sub_key is None else f'{self.key}:{sub_key}'
encoded_value = self.encode(value)
try:
await data_merge(get_edgedb(), key=key, value=encoded_value)
if tx is None:
tx = get_edgedb()

await data_merge(tx, key=key, value=encoded_value)
except Exception as e:
logger.exception(e)
return

async def delete(self, sub_key: str | int | None = None):
async def delete(self, sub_key: str | int | None = None, tx: AsyncIOExecutor | None = None):
key = self.key if sub_key is None else f'{self.key}:{sub_key}'
await data_delete_by_key(get_edgedb(), key=key)

async def get_all(self):
resp = await data_select_key_ilike(get_edgedb(), pattern=f'{self.key}:%')
if tx is None:
tx = get_edgedb()

await data_delete_by_key(tx, key=key)

async def get_all(self, tx: AsyncIOExecutor | None = None):
if tx is None:
tx = get_edgedb()

resp = await data_select_key_ilike(tx, pattern=f'{self.key}:%')
for item in resp:
*_, sub_key = item.key.rpartition(':')
yield sub_key, await self.get(sub_key)
Expand Down
4 changes: 2 additions & 2 deletions nanapi/utils/waicolle.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ async def get_price(self, executor: AsyncIOExecutor, discord_id: int) -> int:
price = await super().get_price(executor, discord_id)
if self.discount_first:
redis_player_key = f'{discord_id}_{get_current_date()}'
if not await user_daily_roll.get(redis_player_key):
if not await user_daily_roll.get(redis_player_key, tx=executor):
price //= 2
return price

Expand Down Expand Up @@ -415,7 +415,7 @@ async def get_price(self, executor: AsyncIOExecutor, discord_id: int) -> int:
if self.discount_first:
curr_date = get_current_date().isocalendar()
redis_player_key = f'{discord_id}_{(curr_date.year, curr_date.week)}'
if not await user_weekly_roll.get(redis_player_key):
if not await user_weekly_roll.get(redis_player_key, tx=executor):
price //= 2
return price

Expand Down

0 comments on commit 02cac41

Please sign in to comment.