|
11 | 11 | from abc import ABC, abstractclassmethod |
12 | 12 | from dataclasses import dataclass, field |
13 | 13 | from functools import lru_cache |
14 | | -from typing import Callable, List, Optional, Tuple, Union |
| 14 | +from typing import Any, Callable, Dict, List, Optional, Tuple, Union |
15 | 15 |
|
16 | 16 | from .exceptions import (EDIDParseError, MaxRetriesExceededError, # noqa:F401 |
17 | 17 | ScreenBrightnessError, format_exc) |
@@ -185,46 +185,45 @@ def _gdi(cls) -> List[dict]: |
185 | 185 | ... |
186 | 186 |
|
187 | 187 |
|
188 | | -class __Cache(dict): |
| 188 | +class __Cache: |
189 | 189 | '''class to cache data with a short shelf life''' |
190 | 190 |
|
191 | 191 | def __init__(self): |
| 192 | + self.logger = logger.getChild(f'{self.__class__.__name__}_{id(self)}') |
192 | 193 | self.enabled = True |
193 | | - super().__init__() |
| 194 | + self._store: Dict[str, Tuple[Any, float]] = {} |
194 | 195 |
|
195 | | - def get(self, key, *args, **kwargs): |
| 196 | + def get(self, key): |
196 | 197 | if not self.enabled: |
197 | 198 | return None |
198 | 199 |
|
199 | 200 | try: |
200 | | - value, expires, orig_args, orig_kwargs = self[key] |
| 201 | + value, expires = self._store[key] |
201 | 202 | if time.time() < expires: |
202 | | - if orig_args == args and orig_kwargs == kwargs: |
203 | | - logger.debug(f'cache get {repr(key)}') |
204 | | - return value |
205 | | - else: |
206 | | - logger.debug(f'cache get {repr(key)} = [expired]') |
207 | | - del self[key] |
| 203 | + self.logger.debug(f'cache get {repr(key)}') |
| 204 | + return value |
| 205 | + self.logger.debug(f'cache get {repr(key)} = [expired]') |
| 206 | + del self._store[key] |
208 | 207 | except KeyError: |
209 | | - logger.debug(f'cache get {repr(key)} = [KeyError]') |
210 | | - pass |
| 208 | + self.logger.debug(f'cache get {repr(key)} = [KeyError]') |
| 209 | + return None |
211 | 210 |
|
212 | | - def store(self, key, value, *args, expires=1, **kwargs): |
213 | | - self[key] = (value, expires + time.time(), args, kwargs) |
214 | | - logger.debug(f'cache set {repr(key)}, expires={expires}') |
| 211 | + def store(self, key, value, expires=1): |
| 212 | + self.logger.debug(f'cache set {repr(key)}, expires={expires}') |
| 213 | + self._store[key] = (value, expires + time.time()) |
215 | 214 |
|
216 | 215 | def expire(self, key=None, startswith=None): |
217 | 216 | if key is not None: |
218 | 217 | try: |
219 | | - del self[key] |
220 | | - logger.debug(f'cache expire key {repr(key)}') |
| 218 | + del self._store[key] |
| 219 | + self.logger.debug(f'cache expire key {repr(key)}') |
221 | 220 | except KeyError: |
222 | 221 | pass |
223 | 222 | elif startswith is not None: |
224 | | - for i in tuple(self.keys()): |
| 223 | + for i in tuple(self._store.keys()): |
225 | 224 | if i.startswith(startswith): |
226 | | - del self[i] |
227 | | - logger.debug(f'cache expire key {repr(i)}') |
| 225 | + del self._store[i] |
| 226 | + self.logger.debug(f'cache expire key {repr(i)}') |
228 | 227 |
|
229 | 228 |
|
230 | 229 | @dataclass |
|
0 commit comments