mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-08-05 02:45:28 +00:00
improve cache eviction logic to handle file access errors and ensure stability
This commit is contained in:
parent
46905ab9b0
commit
9914651cc9
1 changed files with 41 additions and 23 deletions
|
|
@ -98,31 +98,49 @@ class TTSService:
|
||||||
self._enforce_cache_limit()
|
self._enforce_cache_limit()
|
||||||
|
|
||||||
def _enforce_cache_limit(self):
|
def _enforce_cache_limit(self):
|
||||||
"""Evicts oldest files if the cache exceeds the configured byte limit."""
|
"""Evicts oldest files if the cache exceeds the configured byte limit."""
|
||||||
if self.max_cache_bytes <= 0:
|
if self.max_cache_bytes <= 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
files = [f for f in self.cache_dir.glob("*.*") if f.is_file()]
|
try:
|
||||||
total_size = sum(f.stat().st_size for f in files)
|
files = []
|
||||||
|
total_size = 0
|
||||||
|
|
||||||
if total_size > self.max_cache_bytes:
|
# Safely scan files and sum sizes, ignoring files deleted mid-scan
|
||||||
logger.info(f"TTS cache ({total_size} bytes) exceeded limit ({self.max_cache_bytes} bytes). Evicting oldest files.")
|
for f in self.cache_dir.glob("*.*"):
|
||||||
|
try:
|
||||||
# Sort files by modification time (oldest first)
|
if f.is_file():
|
||||||
files.sort(key=lambda f: f.stat().st_mtime)
|
files.append(f)
|
||||||
|
total_size += f.stat().st_size
|
||||||
# Trim down to 80% of max capacity so we aren't constantly triggering this on every new generation
|
except OSError:
|
||||||
target_size = self.max_cache_bytes * 0.8
|
continue
|
||||||
|
|
||||||
while files and total_size > target_size:
|
if total_size > self.max_cache_bytes:
|
||||||
f = files.pop(0)
|
logger.info(
|
||||||
try:
|
f"TTS cache ({total_size} bytes) exceeded limit ({self.max_cache_bytes} bytes). Evicting oldest files."
|
||||||
size = f.stat().st_size
|
)
|
||||||
f.unlink()
|
|
||||||
total_size -= size
|
# Sort files by modification time (oldest first)
|
||||||
except FileNotFoundError:
|
try:
|
||||||
# File was deleted by another process
|
files.sort(key=lambda f: f.stat().st_mtime)
|
||||||
continue
|
except OSError as e:
|
||||||
|
logger.warning(f"Failed to sort cache files by mtime: {e}")
|
||||||
|
|
||||||
|
# Trim down to 80% of max capacity
|
||||||
|
target_size = self.max_cache_bytes * 0.8
|
||||||
|
|
||||||
|
while files and total_size > target_size:
|
||||||
|
f = files.pop(0)
|
||||||
|
try:
|
||||||
|
size = f.stat().st_size
|
||||||
|
f.unlink()
|
||||||
|
total_size -= size
|
||||||
|
except OSError as e:
|
||||||
|
logger.warning(f"Failed to evict cache file {f}: {e}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Error enforcing TTS cache limit: {e}", exc_info=True)
|
||||||
|
|
||||||
def clear_cache(self):
|
def clear_cache(self):
|
||||||
count = 0
|
count = 0
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue