-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.py
More file actions
65 lines (53 loc) · 2.04 KB
/
Copy pathmodule.py
File metadata and controls
65 lines (53 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import typing as t
from ellar.common import IModuleSetup, Module
from ellar.core.conf import Config
from ellar.core.modules import DynamicModule, ModuleBase, ModuleRefBase, ModuleSetup
from ellar.di import ProviderConfig
from .interface import ICacheService
from .model import BaseCacheBackend
from .schema import CacheModuleSchemaSetup
from .service import CacheService
@Module(exports=[ICacheService])
class CacheModule(ModuleBase, IModuleSetup):
"""
Cache Module responsible for setting up cache services.
"""
@classmethod
def _create_dynamic_module(cls, schema: CacheModuleSchemaSetup) -> DynamicModule:
cache_service = CacheService(
t.cast(t.Dict[str, BaseCacheBackend], dict(schema.CACHES))
)
return DynamicModule(
cls,
providers=[
ProviderConfig(CacheService, use_value=cache_service),
ProviderConfig(ICacheService, use_value=cache_service),
],
)
@classmethod
def setup(
cls, default: BaseCacheBackend, **kwargs: BaseCacheBackend
) -> DynamicModule:
args = {"default": default}
args.update(kwargs)
schema = CacheModuleSchemaSetup(**{"CACHES": args})
return cls._create_dynamic_module(schema)
@classmethod
def register_setup(cls) -> ModuleSetup:
return ModuleSetup(cls, inject=[Config], factory=cls.__register_setup_factory)
@staticmethod
def __register_setup_factory(
module_ref: "ModuleRefBase", config: Config
) -> DynamicModule:
if config.CACHES:
schema = CacheModuleSchemaSetup(**{"CACHES": config.CACHES})
module = t.cast(t.Type[CacheModule], module_ref.module)
return module._create_dynamic_module(schema)
cache_service = CacheService()
return DynamicModule(
module_ref.module,
providers=[
ProviderConfig(CacheService, use_value=cache_service),
ProviderConfig(ICacheService, use_value=cache_service),
],
)