-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
117 lines (83 loc) · 2.89 KB
/
cache.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from helper import millis
from i18n import I18NEngine
import database as db
class CacheEntry:
def __init__(self, value, expiration: int):
"""
Cache entry for the cache
:param value: the stored value
:param expiration: the expiration time in milliseconds.
"""
self.value = value
self.creation = millis()
self.expiration = expiration
def has_expired(self):
if self.expiration < 0:
return False
return self.creation + self.expiration < millis()
def value_or_none(self):
"""
A function to get the value if it is not expired
:return: the value or none
"""
if not self.has_expired():
return self.value
return None
class Cache:
def __init__(self):
self._cache: dict[str, CacheEntry] = {}
def cache(self, value, expiration: int = 300000) -> None:
raise NotImplementedError("This cache has not implemented caching")
def get(self, key: str):
get = self._cache.get(key)
if get is None:
return None
return get.value_or_none()
def is_cached(self, key: str):
return self.get(key) is not None
def should_use(self, key: str):
if key is None:
return False
get = self.get(key)
if get is None:
return False
return True
class NothingCache(Cache):
def cache(self, value, expiration: int = 300000) -> None:
if not self.should_use(str(value)):
self._cache[str(value)] = CacheEntry(None, expiration)
return
class I18NCache(Cache):
def cache(self, value: I18NEngine, expiration: int = 300000) -> None:
if not self.should_use(value.language):
self._cache[value.language] = CacheEntry(value, expiration)
return
def get_or_create(self, language: str) -> I18NEngine:
if self.should_use(language):
return self.get(language)
engine = I18NEngine(language)
engine.load()
self.cache(engine)
return engine
class UserIDCache(Cache):
user_check_cache = NothingCache()
def cache(self, value: db.User, expiration: int = -1) -> None:
self._cache[str(value.user_id)] = CacheEntry(str(value.user_id), expiration)
self.user_check_cache.cache(str(value.user_id))
return
def should_use(self, key: str):
value = super().should_use(key)
if key == "None":
return False
if value is True:
return True
if not self.user_check_cache.should_use(key):
query = db.session.query(db.User).filter_by(user_id=int(key)).first()
if query is None:
return False
self.cache(query)
return query is not None
return value
class Globals:
i18n_cache = I18NCache()
user_id_cache = UserIDCache()