mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-08-06 22:10:14 -03:00
fix(cache): deduplicate model entries on add and reconcile to prevent duplicate cards (#1041)
This commit is contained in:
@@ -927,6 +927,25 @@ class ModelScanner:
|
|||||||
# Update cache data
|
# Update cache data
|
||||||
self._cache.raw_data = [item for item in self._cache.raw_data if item['file_path'] not in missing_files]
|
self._cache.raw_data = [item for item in self._cache.raw_data if item['file_path'] not in missing_files]
|
||||||
|
|
||||||
|
dedup_removed = 0
|
||||||
|
seen_paths: set = set()
|
||||||
|
deduped: list = []
|
||||||
|
for item in reversed(self._cache.raw_data):
|
||||||
|
path = item.get('file_path', '')
|
||||||
|
if path not in seen_paths:
|
||||||
|
seen_paths.add(path)
|
||||||
|
deduped.append(item)
|
||||||
|
else:
|
||||||
|
for tag in item.get('tags', []):
|
||||||
|
if tag in self._tags_count:
|
||||||
|
self._tags_count[tag] = max(0, self._tags_count[tag] - 1)
|
||||||
|
if self._tags_count[tag] == 0:
|
||||||
|
del self._tags_count[tag]
|
||||||
|
dedup_removed += 1
|
||||||
|
if dedup_removed > 0:
|
||||||
|
self._cache.raw_data = list(reversed(deduped))
|
||||||
|
total_removed += dedup_removed
|
||||||
|
|
||||||
# Resort cache if changes were made
|
# Resort cache if changes were made
|
||||||
if total_added > 0 or total_removed > 0:
|
if total_added > 0 or total_removed > 0:
|
||||||
# Update folders list
|
# Update folders list
|
||||||
@@ -1352,18 +1371,25 @@ class ModelScanner:
|
|||||||
# Update folder in metadata
|
# Update folder in metadata
|
||||||
metadata_dict['folder'] = folder
|
metadata_dict['folder'] = folder
|
||||||
|
|
||||||
# Add to cache
|
file_path = metadata_dict.get('file_path', '')
|
||||||
self._cache.raw_data.append(metadata_dict)
|
if file_path:
|
||||||
self._cache.add_to_version_index(metadata_dict)
|
old_entries = [item for item in self._cache.raw_data if item.get('file_path') == file_path]
|
||||||
|
for old_entry in old_entries:
|
||||||
|
for tag in old_entry.get('tags', []):
|
||||||
|
if tag in self._tags_count:
|
||||||
|
self._tags_count[tag] = max(0, self._tags_count[tag] - 1)
|
||||||
|
if self._tags_count[tag] == 0:
|
||||||
|
del self._tags_count[tag]
|
||||||
|
self._hash_index.remove_by_path(file_path)
|
||||||
|
self._cache.raw_data = [item for item in self._cache.raw_data if item.get('file_path') != file_path]
|
||||||
|
|
||||||
|
for tag in metadata_dict.get('tags', []):
|
||||||
|
self._tags_count[tag] = self._tags_count.get(tag, 0) + 1
|
||||||
|
|
||||||
|
self._cache.raw_data.append(metadata_dict)
|
||||||
|
|
||||||
# Resort cache data
|
|
||||||
await self._cache.resort()
|
await self._cache.resort()
|
||||||
|
|
||||||
# Update folders list
|
|
||||||
all_folders = set(self._cache.folders)
|
|
||||||
all_folders.add(folder)
|
|
||||||
self._cache.folders = sorted(list(all_folders), key=lambda x: x.lower())
|
|
||||||
|
|
||||||
# Update the hash index
|
# Update the hash index
|
||||||
self._hash_index.add_entry(metadata_dict['sha256'], metadata_dict['file_path'])
|
self._hash_index.add_entry(metadata_dict['sha256'], metadata_dict['file_path'])
|
||||||
await self._persist_current_cache()
|
await self._persist_current_cache()
|
||||||
|
|||||||
Reference in New Issue
Block a user