[ui] ThumbnailCache: reduce number of system calls

This commit is contained in:
mugulmd 2023-01-16 03:43:26 -08:00
parent a2e6a68811
commit 7abbf1bbf3

View file

@ -5,6 +5,7 @@ from PySide2.QtGui import QImageReader, QImageWriter
import os import os
from pathlib import Path from pathlib import Path
import stat
import hashlib import hashlib
import time import time
import logging import logging
@ -173,22 +174,28 @@ class ThumbnailCache(QObject):
# Scan thumbnail directory and gather all thumbnails to remove # Scan thumbnail directory and gather all thumbnails to remove
toRemove = [] toRemove = []
remaining = [] remaining = []
for entry in os.scandir(ThumbnailCache.thumbnailDir): for f_name in os.listdir(ThumbnailCache.thumbnailDir):
if not entry.is_file(): pathname = os.path.join(ThumbnailCache.thumbnailDir, f_name)
# System call to get current item info
f_stat = os.stat(pathname, follow_symlinks=False)
# Check if this is a regular file
if not stat.S_ISREG(f_stat.st_mode):
continue continue
# Compute storage duration since last usage of thumbnail # Compute storage duration since last usage of thumbnail
lastUsage = os.path.getmtime(entry.path) lastUsage = f_stat.st_mtime
storageTime = now - lastUsage storageTime = now - lastUsage
logging.debug(f'[ThumbnailCache] Thumbnail {entry.name} has been stored for {storageTime}s') logging.debug(f'[ThumbnailCache] Thumbnail {f_name} has been stored for {storageTime}s')
if storageTime > ThumbnailCache.storageTimeLimit * 3600 * 24: if storageTime > ThumbnailCache.storageTimeLimit * 3600 * 24:
# Mark as removable if storage time exceeds limit # Mark as removable if storage time exceeds limit
logging.debug(f'[ThumbnailCache] {entry.name} exceeded storage time limit') logging.debug(f'[ThumbnailCache] {f_name} exceeded storage time limit')
toRemove.append(entry.path) toRemove.append(pathname)
else: else:
# Store path and last usage time for potentially sorting and removing later # Store path and last usage time for potentially sorting and removing later
remaining.append((entry.path, lastUsage)) remaining.append((pathname, lastUsage))
# Remove all thumbnails marked as removable # Remove all thumbnails marked as removable
for path in toRemove: for path in toRemove: