Skip to content

Commit

Permalink
fixed loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda committed Oct 25, 2024
1 parent 123ef08 commit d964093
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/cachier/cores/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(
self.separate_files = _update_with_defaults(
separate_files, "separate_files"
)
self._cache_loaded_fpath = ""

@property
def cache_fname(self) -> str:
Expand Down Expand Up @@ -114,6 +115,7 @@ def _load_cache_dict(self) -> Dict[str, CacheEntry]:
try:
with portalocker.Lock(self.cache_fpath, mode="rb") as cf:
cache = pickle.load(cf) # noqa: S301
self._cache_loaded_fpath = str(self.cache_fpath)
except (FileNotFoundError, EOFError):
cache = {}
return {
Expand All @@ -122,11 +124,14 @@ def _load_cache_dict(self) -> Dict[str, CacheEntry]:
}

def get_cache_dict(self, reload: bool = False) -> Dict[str, CacheEntry]:
if self._cache_loaded_fpath != self.cache_fpath:
# force reload if the cache file has changed
# this change is dies to using different wrapped function
reload = True
if self._cache_dict and not (self.reload or reload):
return self._cache_dict
with self.lock:
if not self._cache_dict:
self._cache_dict = self._load_cache_dict()
self._cache_dict = self._load_cache_dict()
return self._cache_dict

def _load_cache_by_key(
Expand Down
24 changes: 13 additions & 11 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,14 @@ def fn_minus(a, b=2):
assert count_p == count_m == 0

for fn, expected in [(fn_plus, 3), (fn_minus, -1)]:
assert cachier_(fn)(1, 2) == expected
assert cachier_(fn)(a=1, b=2) == expected
assert cachier_(fn)(1, 2) == expected, f"for {fn.__name__} inline"
assert cachier_(fn)(a=1, b=2) == expected, f"for {fn.__name__} inline"
assert count_p == 1
assert count_m == 1

for fn, expected in [(fn_plus, 5), (fn_minus, 1)]:
assert cachier_(fn)(3, 2) == expected
assert cachier_(fn)(a=3, b=2) == expected
assert cachier_(fn)(3, 2) == expected, f"for {fn.__name__} inline"
assert cachier_(fn)(a=3, b=2) == expected, f"for {fn.__name__} inline"
assert count_p == 2
assert count_m == 2

Expand All @@ -473,22 +473,24 @@ def fn_minus(a, b=2):

for fn, expected in [(fn_plus, 3), (fn_minus, -1)]:
dummy_ = functools.partial(fn, 1)
assert cachier_(dummy_)() == expected
assert cachier_(dummy_)() == expected, f"for {fn.__name__} wrapped"

dummy_ = functools.partial(fn, 1)
assert cachier_(dummy_)(2) == expected
assert cachier_(dummy_)(2) == expected, f"for {fn.__name__} wrapped"

dummy_ = functools.partial(fn, a=1)
assert cachier_(dummy_)() == expected
assert cachier_(dummy_)() == expected, f"for {fn.__name__} wrapped"

dummy_ = functools.partial(fn, b=2)
assert cachier_(dummy_)(1) == expected
assert cachier_(dummy_)(1) == expected, f"for {fn.__name__} wrapped"

dummy_ = functools.partial(fn, b=2)
assert cachier_(dummy_)(1, b=2) == expected
assert (
cachier_(dummy_)(1, b=2) == expected
), f"for {fn.__name__} wrapped"

assert cachier_(fn)(1, 2) == expected
assert cachier_(fn)(a=1, b=2) == expected
assert cachier_(fn)(1, 2) == expected, f"for {fn.__name__} inline"
assert cachier_(fn)(a=1, b=2) == expected, f"for {fn.__name__} inline"

assert count_p == 1
assert count_m == 1
Expand Down

0 comments on commit d964093

Please sign in to comment.