Skip to content

Commit a399d31

Browse files
committed
Refactor cache module and added cacheModule
1 parent d7d1805 commit a399d31

File tree

12 files changed

+176
-97
lines changed

12 files changed

+176
-97
lines changed

ellar/cache/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from .decorator import cache
12
from .interface import ICacheService
23
from .model import BaseCacheBackend
4+
from .module import CacheModule
35
from .service import CacheService
46

5-
__all__ = ["CacheService", "ICacheService", "BaseCacheBackend"]
7+
__all__ = ["CacheService", "ICacheService", "BaseCacheBackend", "CacheModule", "cache"]

ellar/cache/backends/base.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ def set(
1919
self,
2020
key: str,
2121
value: t.Any,
22-
timeout: t.Union[float, int] = None,
22+
ttl: t.Union[float, int] = None,
2323
version: str = None,
2424
) -> bool:
25-
result = self._cache_client.set(
26-
key, value, int(self.get_backend_timeout(timeout))
27-
)
25+
result = self._cache_client.set(key, value, int(self.get_backend_ttl(ttl)))
2826
if not result:
2927
# Make sure the key doesn't keep its old value in case of failure
3028
# to set (memcached's 1MB limit).
@@ -39,9 +37,9 @@ def delete(self, key: str, version: str = None) -> bool:
3937

4038
@make_key_decorator
4139
def touch(
42-
self, key: str, timeout: t.Union[float, int] = None, version: str = None
40+
self, key: str, ttl: t.Union[float, int] = None, version: str = None
4341
) -> bool:
44-
result = self._cache_client.touch(key, self.get_backend_timeout(timeout))
42+
result = self._cache_client.touch(key, self.get_backend_ttl(ttl))
4543
return bool(result)
4644

4745
@make_key_decorator
@@ -92,22 +90,20 @@ async def set_async(
9290
self,
9391
key: str,
9492
value: t.Any,
95-
timeout: t.Union[float, int] = None,
93+
ttl: t.Union[float, int] = None,
9694
version: str = None,
9795
) -> bool:
98-
result = await self.executor(
99-
self.set, key, value, timeout=timeout, version=version
100-
)
96+
result = await self.executor(self.set, key, value, ttl=ttl, version=version)
10197
return bool(result)
10298

10399
async def delete_async(self, key: str, version: str = None) -> bool:
104100
result = await self.executor(self.delete, key, version=version)
105101
return bool(result)
106102

107103
async def touch_async(
108-
self, key: str, timeout: t.Union[float, int] = None, version: str = None
104+
self, key: str, ttl: t.Union[float, int] = None, version: str = None
109105
) -> bool:
110-
result = await self.executor(self.touch, key, timeout=timeout, version=version)
106+
result = await self.executor(self.touch, key, ttl=ttl, version=version)
111107
return bool(result)
112108

113109
async def close_async(self, **kwargs: t.Any) -> None:

ellar/cache/backends/local_cache.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,16 @@ def set(
2828
self,
2929
key: str,
3030
value: t.Any,
31-
timeout: t.Union[float, int] = None,
31+
ttl: t.Union[float, int] = None,
3232
version: str = None,
3333
) -> bool:
34-
res = self._async_executor(
35-
self.set_async(key, value, timeout=timeout, version=version)
36-
)
34+
res = self._async_executor(self.set_async(key, value, ttl=ttl, version=version))
3735
return bool(res)
3836

3937
def touch(
40-
self, key: str, timeout: t.Union[float, int] = None, version: str = None
38+
self, key: str, ttl: t.Union[float, int] = None, version: str = None
4139
) -> bool:
42-
res = self._async_executor(
43-
self.touch_async(key, timeout=timeout, version=version)
44-
)
40+
res = self._async_executor(self.touch_async(key, ttl=ttl, version=version))
4541
return bool(res)
4642

4743
def incr(self, key: str, delta: int = 1, version: str = None) -> int:
@@ -90,12 +86,12 @@ async def set_async(
9086
self,
9187
key: str,
9288
value: t.Any,
93-
timeout: t.Union[float, int] = None,
89+
ttl: t.Union[float, int] = None,
9490
version: str = None,
9591
) -> bool:
9692
async with self._lock:
9793
self._cache[key] = pickle.dumps(value, self.pickle_protocol)
98-
self._expire_track[key] = self.get_backend_timeout(timeout)
94+
self._expire_track[key] = self.get_backend_ttl(ttl)
9995
return True
10096

10197
def _has_expired(self, key: str) -> bool:
@@ -112,13 +108,13 @@ async def has_key_async(self, key: str, version: str = None) -> bool:
112108

113109
@make_key_decorator
114110
async def touch_async(
115-
self, key: str, timeout: t.Union[float, int] = None, version: str = None
111+
self, key: str, ttl: t.Union[float, int] = None, version: str = None
116112
) -> bool:
117113
async with self._lock:
118114
if self._has_expired(key):
119115
return False
120116

121-
self._expire_track[key] = self.get_backend_timeout(timeout)
117+
self._expire_track[key] = self.get_backend_ttl(ttl)
122118
return True
123119

124120
def has_key(self, key: str, version: str = None) -> bool:

ellar/cache/backends/redis/backend.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ def set(
3232
self,
3333
key: str,
3434
value: t.Any,
35-
timeout: t.Union[float, int] = None,
35+
ttl: t.Union[float, int] = None,
3636
version: str = None,
3737
) -> bool:
38-
res = self._async_executor(
39-
self.set_async(key, value, version=version, timeout=timeout)
40-
)
38+
res = self._async_executor(self.set_async(key, value, version=version, ttl=ttl))
4139
return bool(res)
4240

4341
def touch(
44-
self, key: str, timeout: t.Union[float, int] = None, version: str = None
42+
self, key: str, ttl: t.Union[float, int] = None, version: str = None
4543
) -> bool:
46-
res = self._async_executor(
47-
self.touch_async(key, version=version, timeout=timeout)
48-
)
44+
res = self._async_executor(self.touch_async(key, version=version, ttl=ttl))
4945
return bool(res)
5046

5147
def has_key(self, key: str, version: str = None) -> bool:
@@ -116,14 +112,12 @@ def _get_client(self, *, write: bool = False) -> Redis:
116112
pool = self._get_connection_pool(write)
117113
return self.MEMCACHE_CLIENT(connection_pool=pool)
118114

119-
def get_backend_timeout(
120-
self, timeout: t.Union[float, int] = None
121-
) -> t.Union[float, int]:
122-
if timeout is None:
123-
timeout = self._default_timeout
124-
# The key will be made persistent if None used as a timeout.
115+
def get_backend_ttl(self, ttl: t.Union[float, int] = None) -> t.Union[float, int]:
116+
if ttl is None:
117+
ttl = self._default_ttl
118+
# The key will be made persistent if None used as a ttl.
125119
# Non-positive values will cause the key to be deleted.
126-
return None if timeout is None else max(0, int(timeout))
120+
return None if ttl is None else max(0, int(ttl))
127121

128122
@make_key_decorator
129123
async def get_async(self, key: str, version: str = None) -> t.Any:
@@ -138,15 +132,15 @@ async def set_async(
138132
self,
139133
key: str,
140134
value: t.Any,
141-
timeout: t.Union[float, int] = None,
135+
ttl: t.Union[float, int] = None,
142136
version: str = None,
143137
) -> bool:
144138
client = self._get_client()
145139
value = self._serializer.dumps(value)
146-
if timeout == 0:
140+
if ttl == 0:
147141
await client.delete(key)
148142

149-
return bool(await client.set(key, value, ex=self.get_backend_timeout(timeout)))
143+
return bool(await client.set(key, value, ex=self.get_backend_ttl(ttl)))
150144

151145
@make_key_decorator
152146
async def delete_async(self, key: str, version: str = None) -> bool:
@@ -156,13 +150,13 @@ async def delete_async(self, key: str, version: str = None) -> bool:
156150

157151
@make_key_decorator
158152
async def touch_async(
159-
self, key: str, timeout: t.Union[float, int] = None, version: str = None
153+
self, key: str, ttl: t.Union[float, int] = None, version: str = None
160154
) -> bool:
161155
client = self._get_client()
162-
if timeout is None:
156+
if ttl is None:
163157
res = await client.persist(key)
164158
return bool(res)
165-
res = await client.expire(key, self.get_backend_timeout(timeout))
159+
res = await client.expire(key, self.get_backend_ttl(ttl))
166160
return bool(res)
167161

168162
@make_key_decorator

ellar/common/cache.py renamed to ellar/cache/decorator.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
from functools import wraps
44

55
from ellar.cache.interface import ICacheService
6+
from ellar.common import Context, Provide, extra_args
67
from ellar.core import ExecutionContext, IExecutionContext
78
from ellar.core.params import ExtraEndpointArg
89
from ellar.helper import is_async_callable
910

10-
from .decorators.extra_args import extra_args
11-
from .routing.params import Context, Provide
12-
1311

1412
class _CacheDecorator:
1513
__slots__ = (
@@ -18,7 +16,7 @@ class _CacheDecorator:
1816
"_version",
1917
"_backend",
2018
"_func",
21-
"_timeout",
19+
"_ttl",
2220
"_cache_service_arg",
2321
"_context_arg",
2422
"_make_key_callback",
@@ -27,7 +25,7 @@ class _CacheDecorator:
2725
def __init__(
2826
self,
2927
func: t.Callable,
30-
timeout: t.Union[int, float],
28+
ttl: t.Union[int, float],
3129
*,
3230
key_prefix: str = "",
3331
version: str = None,
@@ -39,7 +37,7 @@ def __init__(
3937
self._version = version
4038
self._backend = backend
4139
self._func = func
42-
self._timeout = timeout
40+
self._ttl = ttl
4341

4442
# create extra args
4543
self._cache_service_arg = ExtraEndpointArg(
@@ -87,7 +85,7 @@ async def _async_wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
8785
await cache_service.set_async(
8886
key,
8987
response,
90-
timeout=self._timeout,
88+
ttl=self._ttl,
9189
version=self._version,
9290
backend=self._backend,
9391
)
@@ -116,7 +114,7 @@ def _wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
116114
cache_service.set(
117115
key,
118116
response,
119-
timeout=self._timeout,
117+
ttl=self._ttl,
120118
version=self._version,
121119
backend=self._backend,
122120
)
@@ -126,7 +124,7 @@ def _wrapper(*args: t.Any, **kwargs: t.Any) -> t.Any:
126124

127125

128126
def cache(
129-
timeout: t.Union[float, int],
127+
ttl: t.Union[float, int],
130128
*,
131129
key_prefix: str = "",
132130
version: str = None,
@@ -135,10 +133,20 @@ def cache(
135133
[t.Union[ExecutionContext, IExecutionContext], str], str
136134
] = None,
137135
) -> t.Callable:
136+
"""
137+
138+
:param ttl: the time to live
139+
:param key_prefix: cache key prefix
140+
:param version: will be used in constructing the key
141+
:param backend: Cache Backend to use. Default is `default`
142+
:param make_key_callback: Key dynamic construct.
143+
:return: TCallable
144+
"""
145+
138146
def _wraps(func: t.Callable) -> t.Callable:
139147
cache_decorator = _CacheDecorator(
140148
func,
141-
timeout,
149+
ttl,
142150
key_prefix=key_prefix,
143151
version=version,
144152
backend=backend,

0 commit comments

Comments
 (0)