diff --git a/jsoncache/ApplicationCache.py b/jsoncache/ApplicationCache.py index b5f7b8c..0a46c96 100644 --- a/jsoncache/ApplicationCache.py +++ b/jsoncache/ApplicationCache.py @@ -59,6 +59,17 @@ class ApplicationCache(): def gen_key(cls, cache_id: str, args: list, kwargs: dict) -> str: return cls.get_hash(args, kwargs, cache_id) + def clean_cache(self, max_age: int = None) -> int: + max_age = max_age or self.default_max_age + cleaned_count = 0 + for cache_path in self.cache_dir.iterdir(): + if cache_path.is_file(): + cache_stat = cache_path.stat() + if cache_stat.st_mtime + max_age <= time.time(): + cache_path.unlink() + cleaned_count += 1 + return cleaned_count + def compress(self, data: str) -> bytes: bin_data = data.encode(self.encoding) if self.compress_data and len(bin_data) > self.compress_threshold: