From b3b3852ff36d893bf13a75467546ba5f851b860f Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Tue, 31 Jan 2023 11:47:53 +0100 Subject: [PATCH] Add no_cache param --- fastapi_book_server/app/filters/book.py | 3 +++ fastapi_book_server/app/services/common.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/fastapi_book_server/app/filters/book.py b/fastapi_book_server/app/filters/book.py index 3b7bce0..b834e6b 100644 --- a/fastapi_book_server/app/filters/book.py +++ b/fastapi_book_server/app/filters/book.py @@ -11,6 +11,7 @@ def get_book_filter( allowed_langs: Optional[list[str]] = Query(None), # type: ignore uploaded_gte: Optional[date] = None, uploaded_lte: Optional[date] = None, + no_cache: bool = False, ) -> dict: result = {} @@ -26,4 +27,6 @@ def get_book_filter( if uploaded_lte: result["uploaded__lte"] = uploaded_lte + result["no_cache"] = no_cache + return result diff --git a/fastapi_book_server/app/services/common.py b/fastapi_book_server/app/services/common.py index 0d5460d..5e830d0 100644 --- a/fastapi_book_server/app/services/common.py +++ b/fastapi_book_server/app/services/common.py @@ -128,28 +128,28 @@ class BaseSearchService(Generic[MODEL, QUERY], BaseService[MODEL, QUERY]): @classmethod async def get_object_ids( - cls, query: QUERY, redis: aioredis.Redis + cls, query: QUERY, redis: aioredis.Redis, no_cache: bool ) -> tuple[int, list[int]]: params = cls.get_raw_params() - if ( + if not no_cache and ( cached_object_ids := await cls.get_cached_ids(query, redis, params) - ) is not None: + ): return cached_object_ids object_ids = await cls._get_object_ids(query) limited_object_ids = object_ids[params.offset : params.offset + params.limit] - if len(object_ids) != 0: + if not no_cache and len(object_ids) != 0: await cls.cache_object_ids(query, object_ids, redis) return len(object_ids), limited_object_ids @classmethod async def get_limited_objects( - cls, query: QUERY, redis: aioredis.Redis + cls, query: QUERY, redis: aioredis.Redis, no_cache: bool ) -> tuple[int, list[MODEL]]: - count, object_ids = await cls.get_object_ids(query, redis) + count, object_ids = await cls.get_object_ids(query, redis, no_cache) queryset: QuerySet[MODEL] = cls.model.objects @@ -164,9 +164,11 @@ class BaseSearchService(Generic[MODEL, QUERY], BaseService[MODEL, QUERY]): @classmethod async def get(cls, query: QUERY, redis: aioredis.Redis) -> Page[MODEL]: + no_cache: bool = query.get("no_cache", False) # type: ignore + params = cls.get_params() - total, objects = await cls.get_limited_objects(query, redis) + total, objects = await cls.get_limited_objects(query, redis, no_cache) return CustomPage.create(items=objects, total=total, params=params)