Skip to content

Commit

Permalink
fix all films and None
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelErsh committed Mar 21, 2024
1 parent 8f36338 commit 3848318
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions async_api/src/services/film.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def __init__(self, redis: Redis, elastic: AsyncElasticsearch):
async def all_films(self, **kwargs: dict[str, Any]) -> list[Film]:
films = await self._films_from_cache(**kwargs)
if not films:
films = await self._get_films_from_elasticsearch(Film)
films = await self._get_films_from_elasticsearch(**kwargs)
if not films:
return []
await self._put_films_to_cache(Film)
await self._put_films_to_cache(films, **kwargs)
return films

def get_all_films_from_elasticsearch(
Expand All @@ -39,7 +39,7 @@ def get_all_films_from_elasticsearch(
genre: str | None = None,
query: str | None = None,
) -> dict | None:
films = Search(get_elastic())
films = Search(using=get_elastic(), index="movies")
if not films:
return None
if genre:
Expand Down Expand Up @@ -89,6 +89,9 @@ async def _film_from_cache(self, film_id: str) -> Film | None:

async def _films_from_cache(self, **kwargs: dict[str, Any]) -> list[Film]:
key = await get_key_by_args(**kwargs)
if not key:
logger.debug("Key not found in the cache")
return None
data = await self.redis.get(key)
if not data:
logger.debug("Film was not found in the cache")
Expand All @@ -97,11 +100,15 @@ async def _films_from_cache(self, **kwargs: dict[str, Any]) -> list[Film]:
return [Film.parse_raw(item) for item in data]

async def _put_film_to_cache(self, film: Film) -> None:
await self.redis.set(film.id, film, FILM_CACHE_EXPIRE_IN_SECONDS)
await self.redis.set(film.id, str(film), FILM_CACHE_EXPIRE_IN_SECONDS)

async def _put_films_to_cache(self, films: list[Film], **search_params: str | Any) -> None:
key = await get_key_by_args(**search_params)
if not key:
return
films_data = [film.dict(by_alias=True) for film in films]
if not films_data:
return
await self.redis.set(key, str(films_data), ex=FILM_CACHE_EXPIRE_IN_SECONDS)


Expand Down

0 comments on commit 3848318

Please sign in to comment.