From b8b2575ad75f8424fb017f5fb6e49ce2efab8498 Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Wed, 1 Jan 2025 03:43:57 -0500 Subject: [PATCH] Limit cache entries to 10 --- auto_editor/analyze.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/auto_editor/analyze.py b/auto_editor/analyze.py index edc1aaa0f..54f1dc826 100644 --- a/auto_editor/analyze.py +++ b/auto_editor/analyze.py @@ -235,6 +235,22 @@ def cache(self, arr: np.ndarray, kind: str, obj: Sequence[object]) -> np.ndarray except Exception as e: self.log.warning(f"Cache write failed: {e}") + cache_entries = [] + with os.scandir(workdir) as entries: + for entry in entries: + if entry.name.endswith(".npz"): + cache_entries.append((entry.path, entry.stat().st_mtime)) + + if len(cache_entries) > 10: + # Sort by modification time, oldest first + cache_entries.sort(key=lambda x: x[1]) + # Remove oldest files until we're back to 10 + for filepath, _ in cache_entries[:-10]: + try: + os.remove(filepath) + except OSError: + pass + return arr def audio(self, stream: int) -> NDArray[np.float32]: