Skip to content

Commit

Permalink
Cache secret fetching from db
Browse files Browse the repository at this point in the history
  • Loading branch information
ishefi committed Mar 6, 2024
1 parent 3d5732d commit e868066
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions logic/game_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime
import heapq
from functools import lru_cache
from typing import TYPE_CHECKING
from typing import no_type_check

Expand Down Expand Up @@ -31,10 +32,18 @@ def __init__(self, session: Session, dt: datetime.date | None = None):
self.session = session

async def get_secret(self) -> str | None:
return self._get_cached_secret(session=self.session, date=self.date)

@staticmethod
@lru_cache
def _get_cached_secret(session: Session, date: datetime.date) -> str | None:
# TODO: this function is accessing db but is NOT ASYNC, which might be
# problematic if we choose to do async stuff with sql in the future.
# the reason for that is `@lru_cache` does not support async.
query = select(tables.SecretWord)
query = query.where(tables.SecretWord.game_date == self.date)
query = query.where(tables.SecretWord.game_date == date)

with hs_transaction(self.session) as session:
with hs_transaction(session) as session:
secret_word = session.exec(query).one_or_none()
if secret_word is not None:
return secret_word.word
Expand Down

0 comments on commit e868066

Please sign in to comment.