Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11.2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this file?

1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 3.11.2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this file?

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
[![license](https://img.shields.io/github/license/long2ice/fastapi-cache)](https://github.com/long2ice/fastapi-cache/blob/main/LICENSE)
[![CI/CD](https://github.com/long2ice/fastapi-cache/actions/workflows/ci-cd.yml/badge.svg)](https://github.com/long2ice/fastapi-cache/actions/workflows/ci-cd.yml)

## What's changed in our fork?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project is not a fork.


1. Add compatibility for pydantic2. See https://github.com/long2ice/fastapi-cache/issues/249 and https://github.com/long2ice/fastapi-cache/issues/236
2. Fix typing broken in https://github.com/yoco-tech/fastapi-cache/commit/013be85f97ae696b97d1d348e4f07d19fda86228
3. Bump up min python version to ^3.8
4. Add pydantic as a requirement
5. Bump up allowed version of redis

## Introduction

`fastapi-cache` is a tool to cache FastAPI endpoint and function results, with
Expand Down
37 changes: 12 additions & 25 deletions fastapi_cache/coder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
import json
import pickle # nosec:B403
from decimal import Decimal
from typing import (
Any,
Callable,
ClassVar,
Dict,
Optional,
TypeVar,
Union,
overload,
)
from typing import Any, Callable, ClassVar, Dict, Optional, TypeVar, Union, overload

import pendulum
from fastapi.encoders import jsonable_encoder
from pydantic import BaseConfig, ValidationError, fields
from pydantic import create_model
from pydantic._internal._model_construction import ModelMetaclass
from starlette.responses import JSONResponse
from starlette.templating import (
_TemplateResponse as TemplateResponse, # pyright: ignore[reportPrivateUsage]
Expand Down Expand Up @@ -64,12 +56,7 @@ def encode(cls, value: Any) -> bytes:
def decode(cls, value: bytes) -> Any:
raise NotImplementedError

# (Shared) cache for endpoint return types to Pydantic model fields.
# Note that subclasses share this cache! If a subclass overrides the
# decode_as_type method and then stores a different kind of field for a
# given type, do make sure that the subclass provides its own class
# attribute for this cache.
_type_field_cache: ClassVar[Dict[Any, fields.ModelField]] = {}
_type_field_cache: ClassVar[Dict[Any, ModelMetaclass]] = {}

@overload
@classmethod
Expand All @@ -89,18 +76,18 @@ def decode_as_type(cls, value: bytes, *, type_: Optional[_T]) -> Union[_T, Any]:

"""
result = cls.decode(value)

if type_ is not None:
try:
field = cls._type_field_cache[type_]
ModelField = cls._type_field_cache[type_]
except KeyError:
field = cls._type_field_cache[type_] = fields.ModelField(
name="body", type_=type_, class_validators=None, model_config=BaseConfig
ModelField = create_model(
'ModelField', value=(type_, ...)
)
result, errors = field.validate(result, {}, loc=())
if errors is not None:
if not isinstance(errors, list):
errors = [errors]
raise ValidationError(errors, type_)
cls._type_field_cache[type_] = ModelField

return ModelField.construct(value=result).value # type: ignore

return result


Expand Down
4 changes: 2 additions & 2 deletions fastapi_cache/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def cache(
key_builder: Optional[KeyBuilder] = None,
namespace: str = "",
injected_dependency_namespace: str = "__fastapi_cache",
) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[Union[R, Response]]]]:
) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]:
"""
cache all function
:param namespace:
Expand All @@ -114,7 +114,7 @@ def cache(

def wrapper(
func: Callable[P, Awaitable[R]]
) -> Callable[P, Awaitable[Union[R, Response]]]:
) -> Callable[P, Awaitable[R]]:
# get_typed_signature ensures that any forward references are resolved first
wrapped_signature = get_typed_signature(func)
to_inject: List[Parameter] = []
Expand Down
Loading