mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-08-08 15:00:15 -03:00
feat(recipes): add cache version counter to model scanners
This commit is contained in:
@@ -242,6 +242,7 @@ class CheckpointScanner(ModelScanner):
|
||||
if entry.get("file_path") == file_path:
|
||||
entry["sha256"] = sha256.lower()
|
||||
entry["hash_status"] = "completed"
|
||||
self.bump_cache_version()
|
||||
break
|
||||
|
||||
logger.info(f"Hash calculated for checkpoint: {file_path}")
|
||||
|
||||
@@ -138,6 +138,9 @@ class ModelLifecycleService:
|
||||
item for item in cache.raw_data if item.get("file_path") != file_path
|
||||
]
|
||||
await cache.resort()
|
||||
bump_cache_version = getattr(self._scanner, "bump_cache_version", None)
|
||||
if callable(bump_cache_version):
|
||||
bump_cache_version()
|
||||
|
||||
if hasattr(self._scanner, "_hash_index") and self._scanner._hash_index:
|
||||
self._scanner._hash_index.remove_by_path(file_path)
|
||||
@@ -244,6 +247,9 @@ class ModelLifecycleService:
|
||||
item for item in cache.raw_data if item["file_path"] != file_path
|
||||
]
|
||||
await cache.resort()
|
||||
bump_cache_version = getattr(self._scanner, "bump_cache_version", None)
|
||||
if callable(bump_cache_version):
|
||||
bump_cache_version()
|
||||
|
||||
excluded = getattr(self._scanner, "_excluded_models", None)
|
||||
if isinstance(excluded, list):
|
||||
|
||||
@@ -79,6 +79,7 @@ class ModelScanner:
|
||||
self.model_class = model_class
|
||||
self.file_extensions = file_extensions
|
||||
self._cache: Any = None
|
||||
self._cache_version: int = 0
|
||||
self._hash_index = hash_index or ModelHashIndex()
|
||||
self._tags_count = {} # Dictionary to store tag counts
|
||||
self._is_initializing = False # Flag to track initialization state
|
||||
@@ -98,6 +99,25 @@ class ModelScanner:
|
||||
# Register this service
|
||||
asyncio.create_task(self._register_service())
|
||||
|
||||
@property
|
||||
def cache_version(self) -> int:
|
||||
"""Monotonic version counter for the in-memory cache.
|
||||
|
||||
Every write path that mutates scanner cache state calls
|
||||
:meth:`bump_cache_version`, so consumers (e.g. RecipeScanner) can
|
||||
detect when a cached derivation of the raw data is stale. Reads never
|
||||
bump.
|
||||
"""
|
||||
return self._cache_version
|
||||
|
||||
def bump_cache_version(self) -> None:
|
||||
"""Invalidate derived caches by incrementing the cache version.
|
||||
|
||||
Public because external services (model lifecycle, route handlers)
|
||||
rewrite scanner raw_data directly and must be able to invalidate it.
|
||||
"""
|
||||
self._cache_version += 1
|
||||
|
||||
def on_library_changed(self) -> None:
|
||||
"""Reset caches when the active library changes."""
|
||||
self._persistent_cache = get_persistent_cache()
|
||||
@@ -107,6 +127,7 @@ class ModelScanner:
|
||||
self._excluded_models = []
|
||||
self._is_initializing = False
|
||||
self._name_display_mode = self._resolve_name_display_mode()
|
||||
self.bump_cache_version()
|
||||
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
@@ -1030,6 +1051,7 @@ class ModelScanner:
|
||||
logger.error(f"{self.model_type.capitalize()} Scanner: Error reconciling cache: {e}", exc_info=True)
|
||||
finally:
|
||||
self._is_initializing = False # Unset flag
|
||||
self.bump_cache_version()
|
||||
|
||||
def is_initializing(self) -> bool:
|
||||
"""Check if the scanner is currently initializing"""
|
||||
@@ -1267,6 +1289,8 @@ class ModelScanner:
|
||||
|
||||
self._log_duplicate_filename_summary()
|
||||
|
||||
self.bump_cache_version()
|
||||
|
||||
def _log_duplicate_filename_summary(self) -> None:
|
||||
"""Log a batched summary of duplicate filename conflicts once per scan."""
|
||||
# Duplicate filename detection is only relevant for LoRAs, which use
|
||||
@@ -1498,6 +1522,7 @@ class ModelScanner:
|
||||
metadata_dict.get('autov3') or None,
|
||||
)
|
||||
await self._persist_current_cache()
|
||||
self.bump_cache_version()
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error adding model to cache: {e}")
|
||||
@@ -1702,6 +1727,7 @@ class ModelScanner:
|
||||
|
||||
if cache_modified:
|
||||
await self._persist_current_cache()
|
||||
self.bump_cache_version()
|
||||
|
||||
if metadata and cache_entry is not None:
|
||||
return cache_entry
|
||||
@@ -1828,6 +1854,7 @@ class ModelScanner:
|
||||
# ---- In-place update of the cache entry ----
|
||||
existing_entry.clear()
|
||||
existing_entry.update(desired_entry)
|
||||
self.bump_cache_version()
|
||||
|
||||
# ---- Incremental tag count update ----
|
||||
new_tags: set[str] = set(desired_entry.get("tags") or [])
|
||||
@@ -1966,6 +1993,7 @@ class ModelScanner:
|
||||
payload['autov3'] = entry['autov3'] or None
|
||||
await MetadataManager.save_metadata(metadata_path, payload)
|
||||
|
||||
self.bump_cache_version()
|
||||
return True
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to update AutoV3 for %s: %s", file_path, exc)
|
||||
@@ -2298,6 +2326,8 @@ class ModelScanner:
|
||||
|
||||
await self._persist_current_cache()
|
||||
|
||||
self.bump_cache_version()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user