mirror of
https://github.com/flibusta-apps/book_library_server.git
synced 2025-12-06 15:15:36 +01:00
Fix query hashing
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import abc
|
import abc
|
||||||
import asyncio
|
import asyncio
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
import hashlib
|
||||||
from random import choice
|
from random import choice
|
||||||
from typing import Generic, Optional, TypedDict, TypeVar, Union
|
from typing import Generic, Optional, TypedDict, TypeVar, Union
|
||||||
|
|
||||||
@@ -8,10 +9,12 @@ from databases import Database
|
|||||||
from fastapi_pagination.api import resolve_params
|
from fastapi_pagination.api import resolve_params
|
||||||
from fastapi_pagination.bases import AbstractParams, RawParams
|
from fastapi_pagination.bases import AbstractParams, RawParams
|
||||||
import meilisearch
|
import meilisearch
|
||||||
|
import orjson
|
||||||
from ormar import Model, QuerySet
|
from ormar import Model, QuerySet
|
||||||
from redis import asyncio as aioredis
|
from redis import asyncio as aioredis
|
||||||
from sqlalchemy import Table
|
from sqlalchemy import Table
|
||||||
|
|
||||||
|
from app.utils.orjson_default import default as orjson_default
|
||||||
from app.utils.pagination import CustomPage, Page
|
from app.utils.pagination import CustomPage, Page
|
||||||
from core.config import env_config
|
from core.config import env_config
|
||||||
|
|
||||||
@@ -47,8 +50,9 @@ class BaseService(Generic[MODEL, QUERY], abc.ABC):
|
|||||||
return cls.CUSTOM_MODEL_CACHE_NAME or cls.model.Meta.tablename
|
return cls.CUSTOM_MODEL_CACHE_NAME or cls.model.Meta.tablename
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_query_hash(query: QUERY) -> int:
|
def _get_query_hash(query: QUERY) -> str:
|
||||||
return hash(frozenset(query.items()))
|
json_value = orjson.dumps(query, orjson_default, option=orjson.OPT_SORT_KEYS)
|
||||||
|
return hashlib.md5(json_value).hexdigest()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_cache_key(cls, query: QUERY) -> str:
|
def get_cache_key(cls, query: QUERY) -> str:
|
||||||
|
|||||||
9
fastapi_book_server/app/utils/orjson_default.py
Normal file
9
fastapi_book_server/app/utils/orjson_default.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
def default(value: Any):
|
||||||
|
if isinstance(value, frozenset):
|
||||||
|
list_value = list(value)
|
||||||
|
return "-".join(sorted(list_value))
|
||||||
|
|
||||||
|
return value
|
||||||
Reference in New Issue
Block a user