|
9 | 9 | import os |
10 | 10 | import pickle # for local caching |
11 | 11 | from datetime import datetime |
12 | | -from typing import Any, Dict, Optional, Tuple |
| 12 | +from typing import Any, Dict, Mapping, Optional, Tuple, Union |
13 | 13 |
|
14 | 14 | import portalocker # to lock on pickle cache IO |
15 | 15 | from watchdog.events import PatternMatchingEventHandler |
@@ -96,28 +96,49 @@ def cache_fpath(self) -> str: |
96 | 96 | os.path.join(os.path.realpath(self.cache_dir), self.cache_fname) |
97 | 97 | ) |
98 | 98 |
|
| 99 | + @staticmethod |
| 100 | + def _convert_legacy_cache_entry( |
| 101 | + entry: Union[dict, CacheEntry], |
| 102 | + ) -> CacheEntry: |
| 103 | + if isinstance(entry, CacheEntry): |
| 104 | + return entry |
| 105 | + return CacheEntry( |
| 106 | + value=entry["value"], |
| 107 | + time=entry["time"], |
| 108 | + stale=entry["stale"], |
| 109 | + _processing=entry["being_calculated"], |
| 110 | + _condition=entry.get("condition", None), |
| 111 | + ) |
| 112 | + |
| 113 | + def _load_cache(self) -> Mapping[str, CacheEntry]: |
| 114 | + try: |
| 115 | + with portalocker.Lock(self.cache_fpath, mode="rb") as cf: |
| 116 | + cache = pickle.load(cf) # noqa: S301 |
| 117 | + except (FileNotFoundError, EOFError): |
| 118 | + cache = {} |
| 119 | + return { |
| 120 | + k: _PickleCore._convert_legacy_cache_entry(v) |
| 121 | + for k, v in cache.items() |
| 122 | + } |
| 123 | + |
99 | 124 | def _reload_cache(self) -> None: |
100 | 125 | with self.lock: |
101 | | - try: |
102 | | - with portalocker.Lock(self.cache_fpath, mode="rb") as cf: |
103 | | - self.cache = pickle.load(cf) # noqa: S301 |
104 | | - except (FileNotFoundError, EOFError): |
105 | | - self.cache = {} |
| 126 | + self.cache = self._load_cache() |
106 | 127 |
|
107 | 128 | def _get_cache(self) -> Dict[str, CacheEntry]: |
108 | | - with self.lock: |
109 | | - if not self.cache: |
110 | | - self._reload_cache() |
111 | | - return self.cache |
| 129 | + if not self.cache: |
| 130 | + self._reload_cache() |
| 131 | + return self.cache |
112 | 132 |
|
113 | 133 | def _get_cache_by_key( |
114 | 134 | self, key=None, hash_str=None |
115 | | - ) -> Optional[Dict[str, CacheEntry]]: |
| 135 | + ) -> Optional[CacheEntry]: |
116 | 136 | fpath = self.cache_fpath |
117 | 137 | fpath += f"_{hash_str or key}" |
118 | 138 | try: |
119 | 139 | with portalocker.Lock(fpath, mode="rb") as cache_file: |
120 | | - return pickle.load(cache_file) # noqa: S301 |
| 140 | + entry = pickle.load(cache_file) # noqa: S301 |
| 141 | + return _PickleCore._convert_legacy_cache_entry(entry) |
121 | 142 | except (FileNotFoundError, EOFError): |
122 | 143 | return None |
123 | 144 |
|
|
0 commit comments