@@ -111,138 +111,4 @@ def is_exceeds_max_size(self, count: int) -> bool:
111
111
return count > self ._max_size
112
112
113
113
def is_allowed_to_cache (self , command : str ) -> bool :
114
- return command in self .DEFAULT_ALLOW_LIST
115
-
116
-
117
- def ensure_string (key ):
118
- if isinstance (key , bytes ):
119
- return key .decode ('utf-8' )
120
- elif isinstance (key , str ):
121
- return key
122
- else :
123
- raise TypeError ("Key must be either a string or bytes" )
124
-
125
-
126
- class CacheMixin :
127
- def __init__ (self ,
128
- use_cache : bool ,
129
- connection_pool : "ConnectionPool" ,
130
- cache : Optional [Cache ] = None ,
131
- cache_size : int = 128 ,
132
- cache_ttl : int = 300 ,
133
- ) -> None :
134
- self .use_cache = use_cache
135
- if not use_cache :
136
- return
137
- if cache is not None :
138
- self .cache = cache
139
- else :
140
- self .cache = TTLCache (maxsize = cache_size , ttl = cache_ttl )
141
- self .keys_mapping = LRUCache (maxsize = 10000 )
142
- self .wrap_connection_pool (connection_pool )
143
- self .connections = []
144
-
145
- def cached_call (self ,
146
- func : Callable [..., ResponseT ],
147
- * args ,
148
- ** options ) -> ResponseT :
149
- if not self .use_cache :
150
- return func (* args , ** options )
151
-
152
- print (f'Cached call with args { args } and options { options } ' )
153
-
154
- keys = None
155
- if 'keys' in options :
156
- keys = options ['keys' ]
157
- if not isinstance (keys , list ):
158
- raise TypeError ("Cache keys must be a list." )
159
- if not keys :
160
- return func (* args , ** options )
161
- print (f'keys { keys } ' )
162
-
163
- cache_key = hashkey (* args )
164
-
165
- for conn in self .connections :
166
- conn .process_invalidation_messages ()
167
-
168
- for key in keys :
169
- if key in self .keys_mapping :
170
- if cache_key not in self .keys_mapping [key ]:
171
- self .keys_mapping [key ].append (cache_key )
172
- else :
173
- self .keys_mapping [key ] = [cache_key ]
174
-
175
- if cache_key in self .cache :
176
- result = self .cache [cache_key ]
177
- print (f'Cached call for { args } yields cached result { result } ' )
178
- return result
179
-
180
- result = func (* args , ** options )
181
- self .cache [cache_key ] = result
182
- print (f'Cached call for { args } yields computed result { result } ' )
183
- return result
184
-
185
- def get_cache_entry (self , * args : Any ) -> Any :
186
- cache_key = hashkey (* args )
187
- return self .cache .get (cache_key , None )
188
-
189
- def invalidate_cache_entry (self , * args : Any ) -> None :
190
- cache_key = hashkey (* args )
191
- if cache_key in self .cache :
192
- self .cache .pop (cache_key )
193
-
194
- def wrap_connection_pool (self , connection_pool : "ConnectionPool" ):
195
- if not self .use_cache :
196
- return
197
- if connection_pool is None :
198
- return
199
- original_maker = connection_pool .make_connection
200
- connection_pool .make_connection = lambda : self ._make_connection (original_maker )
201
-
202
- def _make_connection (self , original_maker : Callable [[], "Connection" ]):
203
- conn = original_maker ()
204
- original_disconnect = conn .disconnect
205
- conn .disconnect = lambda : self ._wrapped_disconnect (conn , original_disconnect )
206
- self .add_connection (conn )
207
- return conn
208
-
209
- def _wrapped_disconnect (self , connection : "Connection" ,
210
- original_disconnect : Callable [[], NoReturn ]):
211
- original_disconnect ()
212
- self .remove_connection (connection )
213
-
214
- def add_connection (self , conn ):
215
- print (f'Tracking connection { conn } { id (conn )} ' )
216
- conn .register_connect_callback (self ._on_connect )
217
- self .connections .append (conn )
218
-
219
- def _on_connect (self , conn ):
220
- conn .send_command ("CLIENT" , "TRACKING" , "ON" )
221
- response = conn .read_response ()
222
- print (f"Client tracking response { response } " )
223
- conn ._parser .set_invalidation_push_handler (self ._cache_invalidation_process )
224
-
225
- def _cache_invalidation_process (
226
- self , data : List [Union [str , Optional [List [str ]]]]
227
- ) -> None :
228
- """
229
- Invalidate (delete) all redis commands associated with a specific key.
230
- `data` is a list of strings, where the first string is the invalidation message
231
- and the second string is the list of keys to invalidate.
232
- (if the list of keys is None, then all keys are invalidated)
233
- """
234
- print (f'Invalidation { data } ' )
235
- if data [1 ] is None :
236
- self .cache .clear ()
237
- else :
238
- for key in data [1 ]:
239
- normalized_key = ensure_string (key )
240
- print (f'Invalidating normalized key { normalized_key } ' )
241
- if normalized_key in self .keys_mapping :
242
- for cache_key in self .keys_mapping [normalized_key ]:
243
- print (f'Invalidating cache key { cache_key } ' )
244
- self .cache .pop (cache_key )
245
-
246
- def remove_connection (self , conn ):
247
- print (f'Untracking connection { conn } { id (conn )} ' )
248
- self .connections .remove (conn )
114
+ return command in self .DEFAULT_ALLOW_LIST
0 commit comments