Skip to content

Commit

Permalink
Separate PickleCache
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Sep 11, 2024
1 parent 0bbcd5a commit 5ea513e
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions findobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@
from elftools.elf import elffile
from collections import defaultdict

def pickle_cache(key, cache_path=".cache", prefix=""):
class PickleCache:
def __init__(self, path=".cache", prefix=""):
self.path = path
self._get_fn = lambda key: os.path.join(path, prefix + key[:24])
def store(self, key, data):
os.makedirs(self.path, exist_ok=True)
with open(self._get_fn(key), "wb") as f:
pickle.dump(data, f)
def load(self, key):
with open(self._get_fn(key), "rb") as f:
return pickle.load(f)

def cached(key, cache):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
cache_key = key(*args, **kwargs)
cache_fn = os.path.join(cache_path, prefix + cache_key[:24])
try:
with open(cache_fn, "rb") as f:
d = pickle.load(f)
d = cache.load(cache_key)
if d["key"] != cache_key:
raise Exception("Cache key mismatch")
return d["data"]
except Exception as e:
res = func(*args, **kwargs)
try:
os.makedirs(cache_path, exist_ok=True)
with open(cache_fn, "wb") as f:
pickle.dump({
"key": cache_key,
"data": res,
}, f)
cache.store(cache_key, {
"key": cache_key,
"data": res,
})
except Exception as e:
pass
return res
Expand All @@ -55,7 +63,7 @@ def _cache_key(self):
digest.update(bytes.fromhex("45155db4bc868fa78cb99c3448b2bf2b"))
return digest.hexdigest()

@pickle_cache(key=_cache_key, prefix="ar_")
@cached(key=_cache_key, cache=PickleCache(prefix="ar_"))
def load_symbols(self):
print("Loading", self.fn)
objs = defaultdict(lambda: {"def": set(), "undef": set(), "weak": set()})
Expand Down

0 comments on commit 5ea513e

Please sign in to comment.