-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to cache with a custom key? #329
Comments
I guess I could just do: c = FanoutCache(cache_dir)
def compute_file_hash(file_path: Path) -> str:
hash_func = hashlib.sha256()
with file_path.open("rb") as f:
while chunk := f.read(8192):
hash_func.update(chunk)
return hash_func.hexdigest()
def cache_mine(method):
def wrapper(*args, **kwargs):
file_path = args[0]
assert isinstance(file_path, Path), f"Expected Path, got {type(file_path)}"
file_hash = compute_file_hash(file_path)
with c as reference:
if file_hash in reference:
return reference[file_hash]
out = method(*args, **kwargs)
if out is not None:
reference.set(file_hash, out)
return out
return wrapper
@cache_mine
def process_file(file_path: Path):
print("Processing!!")
return -1 |
Override the see more: python-diskcache/diskcache/core.py Lines 1864 to 1888 in ebfa37c
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm looking for a way to cache the output of processing a file, but I want to use the hash of the file, not the path. This code doesn't work, but it would be more or less what I would need, is it possible to do anything similar with this library?
The text was updated successfully, but these errors were encountered: