|
46 | 46 |
|
47 | 47 | filename_list_cache: dict[str, tuple[list[str], dict[str, float], float]] = {}
|
48 | 48 |
|
| 49 | +class CacheHelper: |
| 50 | + """ |
| 51 | + Helper class for managing file list cache data. |
| 52 | + """ |
| 53 | + def __init__(self): |
| 54 | + self.cache: dict[str, tuple[list[str], dict[str, float], float]] = {} |
| 55 | + self.active = False |
| 56 | + |
| 57 | + def get(self, key: str, default=None) -> tuple[list[str], dict[str, float], float]: |
| 58 | + if not self.active: |
| 59 | + return default |
| 60 | + return self.cache.get(key, default) |
| 61 | + |
| 62 | + def set(self, key: str, value: tuple[list[str], dict[str, float], float]) -> None: |
| 63 | + if self.active: |
| 64 | + self.cache[key] = value |
| 65 | + |
| 66 | + def clear(self): |
| 67 | + self.cache.clear() |
| 68 | + |
| 69 | + def __enter__(self): |
| 70 | + self.active = True |
| 71 | + return self |
| 72 | + |
| 73 | + def __exit__(self, exc_type, exc_value, traceback): |
| 74 | + self.active = False |
| 75 | + self.clear() |
| 76 | + |
| 77 | +cache_helper = CacheHelper() |
| 78 | + |
49 | 79 | extension_mimetypes_cache = {
|
50 | 80 | "webp" : "image",
|
51 | 81 | }
|
@@ -257,6 +287,10 @@ def get_filename_list_(folder_name: str) -> tuple[list[str], dict[str, float], f
|
257 | 287 | return sorted(list(output_list)), output_folders, time.perf_counter()
|
258 | 288 |
|
259 | 289 | def cached_filename_list_(folder_name: str) -> tuple[list[str], dict[str, float], float] | None:
|
| 290 | + strong_cache = cache_helper.get(folder_name) |
| 291 | + if strong_cache is not None: |
| 292 | + return strong_cache |
| 293 | + |
260 | 294 | global filename_list_cache
|
261 | 295 | global folder_names_and_paths
|
262 | 296 | folder_name = map_legacy(folder_name)
|
@@ -285,6 +319,7 @@ def get_filename_list(folder_name: str) -> list[str]:
|
285 | 319 | out = get_filename_list_(folder_name)
|
286 | 320 | global filename_list_cache
|
287 | 321 | filename_list_cache[folder_name] = out
|
| 322 | + cache_helper.set(folder_name, out) |
288 | 323 | return list(out[0])
|
289 | 324 |
|
290 | 325 | def get_save_image_path(filename_prefix: str, output_dir: str, image_width=0, image_height=0) -> tuple[str, str, int, str, str]:
|
|
0 commit comments