Skip to content

Commit a378f4d

Browse files
committed
Added CACHES to ellar Config
1 parent 9a00458 commit a378f4d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

ellar/core/conf/app_settings_models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .mixins import (
1515
ConfigDefaultTypesMixin,
16+
TBaseCacheBackend,
1617
TEventHandler,
1718
TExceptionHandler,
1819
TMiddleware,
@@ -120,6 +121,7 @@ class Config:
120121
TEMPLATE_GLOBAL_FILTERS: t.Dict[str, t.Callable[..., t.Any]] = {}
121122

122123
LOGGING: t.Optional[t.Dict[str, t.Any]] = None
124+
CACHES: t.Dict[str, TBaseCacheBackend] = {}
123125

124126
@validator("MIDDLEWARE", pre=True)
125127
def pre_middleware_validate(cls, value: t.Any) -> t.Any:
@@ -137,3 +139,9 @@ def serializer_custom_encoder(cls, value: t.Any) -> t.Any:
137139
def pre_static_mount_path(cls, value: t.Any) -> t.Any:
138140
assert value.startswith("/"), "Routed paths must start with '/'"
139141
return value
142+
143+
@validator("CACHES", pre=True)
144+
def pre_cache_validate(cls, value: t.Dict) -> t.Any:
145+
if value and not value.get("default"):
146+
raise ValueError("CACHES configuration must have a 'default' key")
147+
return value

ellar/core/conf/mixins.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from starlette.responses import JSONResponse
55
from starlette.types import ASGIApp
66

7+
from ellar.cache.model import BaseCacheBackend
78
from ellar.constants import LOG_LEVELS as log_levels
89
from ellar.core.events import EventHandler
910
from ellar.core.exceptions.interfaces import IExceptionHandler
@@ -19,9 +20,25 @@
1920
"TMiddleware",
2021
"TEventHandler",
2122
"TExceptionHandler",
23+
"TBaseCacheBackend",
2224
]
2325

2426

27+
class TBaseCacheBackend:
28+
@classmethod
29+
def __get_validators__(
30+
cls: t.Type["TBaseCacheBackend"],
31+
) -> t.Iterable[t.Callable[..., t.Any]]:
32+
yield cls.validate
33+
34+
@classmethod
35+
def validate(cls: t.Type["TBaseCacheBackend"], v: t.Any) -> t.Any:
36+
if isinstance(v, BaseCacheBackend):
37+
return v
38+
39+
raise ValueError(f"Expected BaseCacheBackend, received: {type(v)}")
40+
41+
2542
class TExceptionHandler:
2643
@classmethod
2744
def __get_validators__(
@@ -162,3 +179,5 @@ class ConfigDefaultTypesMixin:
162179
# TrustHostMiddleware setup
163180
ALLOWED_HOSTS: t.List[str]
164181
REDIRECT_HOST: bool
182+
183+
CACHES: t.Dict[str, TBaseCacheBackend]

0 commit comments

Comments
 (0)