From 6b0adb5d688bb2b2fda54e8a270a9913596430eb Mon Sep 17 00:00:00 2001 From: Julian Hammer Date: Fri, 6 Nov 2020 12:27:34 +0100 Subject: [PATCH] improved cache handing (always hashing original file) --- osaca/data/_build_cache.py | 0 osaca/semantics/hw_model.py | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 15 deletions(-) mode change 100644 => 100755 osaca/data/_build_cache.py diff --git a/osaca/data/_build_cache.py b/osaca/data/_build_cache.py old mode 100644 new mode 100755 diff --git a/osaca/semantics/hw_model.py b/osaca/semantics/hw_model.py index de47ae7..c742be4 100755 --- a/osaca/semantics/hw_model.py +++ b/osaca/semantics/hw_model.py @@ -316,17 +316,17 @@ class MachineModel(object): :returns: cached DB if existing, `False` otherwise """ p = Path(filepath) - # 1. companion cachefile: same location, with '.' prefix and '.pickle' suffix - companion_cachefile = p.with_name('.' + p.name).with_suffix('.pickle') - if companion_cachefile.exists(): - if companion_cachefile.stat().st_mtime > p.stat().st_mtime: - # companion file up-to-date - with companion_cachefile.open('rb') as f: - return pickle.load(f) - - # 2. home cachefile: ~/.osaca/cache/.pickle hexhash = hashlib.sha256(p.read_bytes()).hexdigest() - home_cachefile = (Path(utils.CACHE_DIR) / hexhash).with_suffix('.pickle') + + # 1. companion cachefile: same location, with '._.pickle' + companion_cachefile = p.with_name('.' + p.stem + '_' + hexhash).with_suffix('.pickle') + if companion_cachefile.exists(): + # companion file (must be up-to-date, due to equal hash) + with companion_cachefile.open('rb') as f: + return pickle.load(f) + + # 2. home cachefile: ~/.osaca/cache/_.pickle + home_cachefile = (Path(utils.CACHE_DIR) / (p.stem + '_' + hexhash)).with_suffix('.pickle') if home_cachefile.exists(): # home file (must be up-to-date, due to equal hash) with home_cachefile.open('rb') as f: @@ -343,21 +343,21 @@ class MachineModel(object): :type data: :class:`dict` """ p = Path(filepath) - # 1. companion cachefile: same location, with '.' prefix and '.pickle' suffix - companion_cachefile = p.with_name('.' + p.name).with_suffix('.pickle') + hexhash = hashlib.sha256(p.read_bytes()).hexdigest() + # 1. companion cachefile: same location, with '._.pickle' + companion_cachefile = p.with_name('.' + p.stem + '_' + hexhash).with_suffix('.pickle') if os.access(str(companion_cachefile.parent), os.W_OK): with companion_cachefile.open('wb') as f: pickle.dump(data, f) return - # 2. home cachefile: ~/.osaca/cache/.pickle - hexhash = hashlib.sha256(p.read_bytes()).hexdigest() + # 2. home cachefile: ~/.osaca/cache/_.pickle cache_dir = Path(utils.CACHE_DIR) try: os.makedirs(cache_dir, exist_ok=True) except OSError: return - home_cachefile = (cache_dir / hexhash).with_suffix('.pickle') + home_cachefile = (cache_dir / (p.stem + '_' + hexhash)).with_suffix('.pickle') if os.access(str(home_cachefile.parent), os.W_OK): with home_cachefile.open('wb') as f: pickle.dump(data, f)