-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodel.py
More file actions
82 lines (69 loc) · 2.59 KB
/
Copy pathmodel.py
File metadata and controls
82 lines (69 loc) · 2.59 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import time
import typing as t
import warnings
from abc import ABC
from .interface import IBaseCacheBackendAsync, IBaseCacheBackendSync
class CacheKeyWarning(RuntimeWarning):
pass
class BaseCacheBackend(IBaseCacheBackendSync, IBaseCacheBackendAsync, ABC):
MEMCACHE_MAX_KEY_LENGTH = 250
def __init__(
self,
key_prefix: t.Optional[str] = None,
version: t.Optional[int] = None,
ttl: t.Optional[int] = None,
) -> None:
self._key_prefix = key_prefix or ""
self._version = version or 1
self._default_ttl = int(ttl) if ttl else 300
@property
def key_prefix(self) -> str:
return self._key_prefix
async def has_key_async(self, key: str, version: t.Optional[str] = None) -> bool:
"""
Return True if the key is in the cache and has not expired.
"""
return await self.get_async(key, version=version) is not None
def has_key(self, key: str, version: t.Optional[str] = None) -> bool:
"""
Return True if the key is in the cache and has not expired.
"""
return self.get(key, version=version) is not None
def validate_key(self, key: str) -> None:
if len(key) > self.MEMCACHE_MAX_KEY_LENGTH:
warnings.warn(
"Cache key will cause errors if used with memcached: %r "
"(longer than %s)" % (key, self.MEMCACHE_MAX_KEY_LENGTH),
CacheKeyWarning,
stacklevel=3,
)
def _memcache_key_warnings(self, key: str) -> None:
for char in key:
if ord(char) < 33 or ord(char) == 127:
warnings.warn(
"Cache key contains characters that will cause errors if "
"used with memcached: %r" % key,
CacheKeyWarning,
stacklevel=3,
)
break
def make_key(self, key: str, version: t.Optional[str] = None) -> str:
"""
Default function to generate keys.
Construct the key used by all other methods. By default, prepend
the `key_prefix`.
"""
return "%s:%s:%s" % (self._key_prefix, version or self._version, key)
def get_backend_ttl(
self, ttl: t.Union[float, int, None] = None
) -> t.Union[float, int]:
"""
Return the timeout value usable by this backend based upon the provided
timeout.
"""
if ttl is None:
ttl = self._default_ttl
elif ttl == 0:
# avoid time.time() related precision issues
ttl = -1
return time.time() + ttl