From 074d75770ea1f2ea9d93d0ac3ba166d25357fedb Mon Sep 17 00:00:00 2001 From: pax Date: Sun, 5 Apr 2026 01:09:17 -0500 Subject: [PATCH] Auto-evict cache after each image download When auto_evict is enabled and cache exceeds max_cache_mb, evicts oldest non-favorited files immediately after download. --- booru_viewer/gui/app.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/booru_viewer/gui/app.py b/booru_viewer/gui/app.py index b7b556f..c454c84 100644 --- a/booru_viewer/gui/app.py +++ b/booru_viewer/gui/app.py @@ -884,6 +884,25 @@ class BooruApp(QMainWindow): if 0 <= idx < len(self._grid._thumbs): self._grid._thumbs[idx]._cached_path = path self._update_fullscreen(path, info) + # Auto-evict if over cache limit + self._auto_evict_cache() + + def _auto_evict_cache(self) -> None: + if not self._db.get_setting_bool("auto_evict"): + return + max_mb = self._db.get_setting_int("max_cache_mb") + if max_mb <= 0: + return + max_bytes = max_mb * 1024 * 1024 + current = cache_size_bytes(include_thumbnails=False) + if current > max_bytes: + protected = set() + for fav in self._db.get_favorites(limit=999999): + if fav.cached_path: + protected.add(fav.cached_path) + evicted = evict_oldest(max_bytes, protected) + if evicted: + log.info(f"Auto-evicted {evicted} cached files") def _on_favorite_selected(self, fav) -> None: self._status.showMessage(f"Favorite #{fav.post_id}")