diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..8459b7c --- /dev/null +++ b/TODO.md @@ -0,0 +1,10 @@ +# TODO List + +These below are from Issues or PRs in the original repository. + +- Add ability to manually expire a cache entry + () +- Add support for caching non-FastAPI functions + () +- Take a look at other issues in the original repository to see if any need to be +added here. diff --git a/fastapi_redis_cache/util.py b/fastapi_redis_cache/util.py index a49b22c..3dc47fa 100644 --- a/fastapi_redis_cache/util.py +++ b/fastapi_redis_cache/util.py @@ -3,9 +3,12 @@ import json from datetime import date, datetime from decimal import Decimal -from typing import Any, Union +from enum import Enum +from typing import Any, Callable, Union +from uuid import UUID from dateutil import parser +from pydantic import BaseModel DATETIME_AWARE = "%m/%d/%Y %I:%M:%S %p %z" DATE_ONLY = "%m/%d/%Y" @@ -22,21 +25,37 @@ str(Decimal): Decimal, } +HandlerType = Callable[[Any], Union[dict[str, str], str]] + class BetterJsonEncoder(json.JSONEncoder): """Subclass the JSONEncoder to handle more types.""" def default(self, obj: Any) -> Union[dict[str, str], Any]: # noqa: ANN401 - """Return a serializable object for the JSONEncoder to use.""" - if isinstance(obj, datetime): - return { - "val": obj.strftime(DATETIME_AWARE), + """Return a serializable object for the JSONEncoder to use. + + This is re-written from the original code to handle more types, and not + end up with a mass of if-else and return statements. + """ + type_mapping: dict[type, HandlerType] = { + datetime: lambda o: { + "val": o.strftime(DATETIME_AWARE), "_spec_type": str(datetime), - } - if isinstance(obj, date): - return {"val": obj.strftime(DATE_ONLY), "_spec_type": str(date)} - if isinstance(obj, Decimal): - return {"val": str(obj), "_spec_type": str(Decimal)} + }, + date: lambda o: { + "val": o.strftime(DATE_ONLY), + "_spec_type": str(date), + }, + Decimal: lambda o: {"val": str(o), "_spec_type": str(Decimal)}, + BaseModel: lambda o: o.model_dump(), + UUID: lambda o: str(o), + Enum: lambda o: str(o.value), + } + + for obj_type, handler in type_mapping.items(): + if isinstance(obj, obj_type): + return handler(obj) + return super().default(obj) diff --git a/fastapi_redis_cache/version.py b/fastapi_redis_cache/version.py deleted file mode 100644 index 24b8e3e..0000000 --- a/fastapi_redis_cache/version.py +++ /dev/null @@ -1,4 +0,0 @@ -"""Define Version information for the package.""" - -__version_info__ = ("0", "2", "5") -__version__ = ".".join(__version_info__) diff --git a/requirements-dev.txt b/requirements-dev.txt index fbf04d7..c45c992 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -47,7 +47,7 @@ pydantic-extra-types==2.6.0 ; python_version >= "3.9" and python_version < "4.0" pydantic-settings==2.2.1 ; python_version >= "3.9" and python_version < "4.0" pydantic==2.6.4 ; python_version >= "3.9" and python_version < "4.0" pyfakefs==5.3.5 ; python_version >= "3.9" and python_version < "4.0" -pymarkdownlnt==0.9.17 ; python_version >= "3.9" and python_version < "4.0" +pymarkdownlnt==0.9.18 ; python_version >= "3.9" and python_version < "4.0" pytest-asyncio==0.21.1 ; python_version >= "3.9" and python_version < "4.0" pytest-cov==4.1.0 ; python_version >= "3.9" and python_version < "4.0" pytest-env==1.1.3 ; python_version >= "3.9" and python_version < "4.0"