From 51acc885fa5eb8bc42e645371d4e3d5384c59811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E9=B9=8F?= Date: Wed, 26 Jun 2024 17:01:22 +0800 Subject: [PATCH] Delete specific cache and check if cache exists --- src/persist_cache/caching.py | 17 +++++++++++++++++ src/persist_cache/persist_cache.py | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/persist_cache/caching.py b/src/persist_cache/caching.py index da9fd5d..245cc8b 100644 --- a/src/persist_cache/caching.py +++ b/src/persist_cache/caching.py @@ -49,6 +49,23 @@ def get(key: str, dir: str, expiry: Union[int, float, timedelta, None] = None) - with open(path, 'rb') as file: return deserialize(file.read()) +def remove(key: str, dir: str) -> None: + """Remove the given key of the provided cache to the specified value.""" + + path = f'{dir}/{key}.msgpack' + + # Lock the entry before writing to it. + with FileLock(f'{path}.lock'): + if os.path.exists(path): + os.remove(path) + +def exists(key: str, dir: str) -> bool: + """Check if the given key exists in the provided cache.""" + + path = f'{dir}/{key}.msgpack' + + return os.path.exists(path) + def hash(data: Any) -> str: """Hash the given data.""" diff --git a/src/persist_cache/persist_cache.py b/src/persist_cache/persist_cache.py index 2a8e0b3..74459b4 100644 --- a/src/persist_cache/persist_cache.py +++ b/src/persist_cache/persist_cache.py @@ -185,11 +185,37 @@ def set_expiry(value: Union[int, float, timedelta, None]) -> None: expiry = value + def delete_once(*args, **kwargs) -> None: + """Remove the cache.""" + nonlocal dir, expiry, is_method + + # Map arguments to their keywords or the keyword of the args parameter where necessary, filtering out the first argument if the function is a method, to enable the consistent caching of function calls where positional arguments are used on some occasions and keyword arguments are used on others. + arguments = inflate_arguments(signature, args_parameter, args_i, args[is_method:], kwargs) + + # Hash the arguments to produce the cache key. + key = caching.hash(arguments) + + caching.remove(key, dir) + + def exists(*args, **kwargs) -> None: + """Check if cache exists""" + nonlocal dir, expiry, is_method + + # Map arguments to their keywords or the keyword of the args parameter where necessary, filtering out the first argument if the function is a method, to enable the consistent caching of function calls where positional arguments are used on some occasions and keyword arguments are used on others. + arguments = inflate_arguments(signature, args_parameter, args_i, args[is_method:], kwargs) + + # Hash the arguments to produce the cache key. + key = caching.hash(arguments) + + return caching.exists(key, dir) + wrapper.delete_cache = delete_cache wrapper.clear_cache = clear_cache wrapper.cache_clear = wrapper.clear_cache # Add an alias for cache_clear which is used by lru_cache. wrapper.flush_cache = flush_cache wrapper.set_expiry = set_expiry + wrapper.delete_once = delete_once + wrapper.exists = exists # Preserve the original function. wrapper.__wrapped__ = func