-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dto-codegen
- Loading branch information
Showing
19 changed files
with
316 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Generic, Protocol, TypeVar | ||
|
||
from litestar.utils.deprecation import deprecated | ||
|
||
if TYPE_CHECKING: | ||
from sqlalchemy import Engine | ||
from sqlalchemy.ext.asyncio import AsyncEngine | ||
|
||
|
||
EngineT_co = TypeVar("EngineT_co", bound="Engine | AsyncEngine", covariant=True) | ||
|
||
|
||
class HasGetEngine(Protocol[EngineT_co]): | ||
def get_engine(self) -> EngineT_co: | ||
... | ||
|
||
|
||
class _CreateEngineMixin(Generic[EngineT_co]): | ||
@deprecated(version="2.1.1", removal_in="3.0.0", alternative="get_engine()") | ||
def create_engine(self: HasGetEngine[EngineT_co]) -> EngineT_co: | ||
return self.get_engine() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from __future__ import annotations | ||
|
||
from msgspec.msgpack import encode as encode_msgpack | ||
|
||
from litestar.enums import ScopeType | ||
from litestar.utils import get_litestar_scope_state | ||
|
||
from .base import AbstractMiddleware | ||
|
||
__all__ = ["ResponseCacheMiddleware"] | ||
|
||
from typing import TYPE_CHECKING, cast | ||
|
||
from litestar import Request | ||
from litestar.constants import SCOPE_STATE_IS_CACHED | ||
|
||
if TYPE_CHECKING: | ||
from litestar.config.response_cache import ResponseCacheConfig | ||
from litestar.handlers import HTTPRouteHandler | ||
from litestar.types import ASGIApp, Message, Receive, Scope, Send | ||
|
||
|
||
class ResponseCacheMiddleware(AbstractMiddleware): | ||
def __init__(self, app: ASGIApp, config: ResponseCacheConfig) -> None: | ||
self.config = config | ||
super().__init__(app=app, scopes={ScopeType.HTTP}) | ||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
route_handler = cast("HTTPRouteHandler", scope["route_handler"]) | ||
store = self.config.get_store_from_app(scope["app"]) | ||
|
||
expires_in: int | None = None | ||
if route_handler.cache is True: | ||
expires_in = self.config.default_expiration | ||
elif route_handler.cache is not False and isinstance(route_handler.cache, int): | ||
expires_in = route_handler.cache | ||
|
||
messages = [] | ||
|
||
async def wrapped_send(message: Message) -> None: | ||
if not get_litestar_scope_state(scope, SCOPE_STATE_IS_CACHED): | ||
messages.append(message) | ||
if message["type"] == "http.response.body" and not message["more_body"]: | ||
key = (route_handler.cache_key_builder or self.config.key_builder)(Request(scope)) | ||
await store.set(key, encode_msgpack(messages), expires_in=expires_in) | ||
await send(message) | ||
|
||
await self.app(scope, receive, wrapped_send) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.