Skip to content

Commit 84fe27a

Browse files
Bordashaypal5
andauthored
fix reusing legacy pickle caches (#249)
* fix reusing legacy pickle caches * typing --------- Co-authored-by: Shay Palachy-Affek <shaypal5@users.noreply.github.com>
1 parent 040e981 commit 84fe27a

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

src/cachier/cores/pickle.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import os
1010
import pickle # for local caching
1111
from datetime import datetime
12-
from typing import Any, Dict, Optional, Tuple
12+
from typing import Any, Dict, Mapping, Optional, Tuple, Union
1313

1414
import portalocker # to lock on pickle cache IO
1515
from watchdog.events import PatternMatchingEventHandler
@@ -96,28 +96,49 @@ def cache_fpath(self) -> str:
9696
os.path.join(os.path.realpath(self.cache_dir), self.cache_fname)
9797
)
9898

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+
99124
def _reload_cache(self) -> None:
100125
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()
106127

107128
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
112132

113133
def _get_cache_by_key(
114134
self, key=None, hash_str=None
115-
) -> Optional[Dict[str, CacheEntry]]:
135+
) -> Optional[CacheEntry]:
116136
fpath = self.cache_fpath
117137
fpath += f"_{hash_str or key}"
118138
try:
119139
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)
121142
except (FileNotFoundError, EOFError):
122143
return None
123144

0 commit comments

Comments
 (0)