Skip to content

Commit

Permalink
fix for #198
Browse files Browse the repository at this point in the history
  • Loading branch information
OnnoEbbens committed Jul 7, 2023
1 parent 6f3ea00 commit 4509fe8
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions nlmod/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,16 @@ def decorator(*args, cachedir=None, cachename=None, **kwargs):

# only use cache if the cache file and the pickled function arguments exist
if os.path.exists(fname_cache) and os.path.exists(fname_pickle_cache):
with open(fname_pickle_cache, "rb") as f:
func_args_dic_cache = pickle.load(f)
# check if you can read the pickle, there are several reasons why a
# pickle can not be read.
try:
with open(fname_pickle_cache, "rb") as f:
func_args_dic_cache = pickle.load(f)
pickle_check = True
except (pickle.UnpicklingError, ModuleNotFoundError):
logger.info(f"could not read pickle, not using cache")
pickle_check = False
argument_check = False

# check if the module where the function is defined was changed
# after the cache was created
Expand All @@ -133,16 +141,17 @@ def decorator(*args, cachedir=None, cachename=None, **kwargs):

cached_ds = xr.open_dataset(fname_cache, mask_and_scale=False)

# add netcdf hash to function arguments dic, see #66
func_args_dic["_nc_hash"] = dask.base.tokenize(cached_ds)
if pickle_check:
# add netcdf hash to function arguments dic, see #66
func_args_dic["_nc_hash"] = dask.base.tokenize(cached_ds)

# check if cache was created with same function arguments as
# function call
argument_check = _same_function_arguments(
func_args_dic, func_args_dic_cache
)
# check if cache was created with same function arguments as
# function call
argument_check = _same_function_arguments(
func_args_dic, func_args_dic_cache
)

if modification_check and argument_check:
if modification_check and argument_check and pickle_check:
if dataset is None:
logger.info(f"using cached data -> {cachename}")
return cached_ds
Expand Down

0 comments on commit 4509fe8

Please sign in to comment.