Skip to content

Commit a1e71cf

Browse files
authored
very simple strong-cache on model list (#4969)
* very simple strong-cache on model list * store the cache after validation too * only cache object_info for now * use a 'with' context
1 parent 0bfc7cc commit a1e71cf

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

folder_paths.py

+35
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@
4646

4747
filename_list_cache: dict[str, tuple[list[str], dict[str, float], float]] = {}
4848

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+
4979
extension_mimetypes_cache = {
5080
"webp" : "image",
5181
}
@@ -257,6 +287,10 @@ def get_filename_list_(folder_name: str) -> tuple[list[str], dict[str, float], f
257287
return sorted(list(output_list)), output_folders, time.perf_counter()
258288

259289
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+
260294
global filename_list_cache
261295
global folder_names_and_paths
262296
folder_name = map_legacy(folder_name)
@@ -285,6 +319,7 @@ def get_filename_list(folder_name: str) -> list[str]:
285319
out = get_filename_list_(folder_name)
286320
global filename_list_cache
287321
filename_list_cache[folder_name] = out
322+
cache_helper.set(folder_name, out)
288323
return list(out[0])
289324

290325
def get_save_image_path(filename_prefix: str, output_dir: str, image_width=0, image_height=0) -> tuple[str, str, int, str, str]:

server.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,15 @@ def node_info(node_class):
552552

553553
@routes.get("/object_info")
554554
async def get_object_info(request):
555-
out = {}
556-
for x in nodes.NODE_CLASS_MAPPINGS:
557-
try:
558-
out[x] = node_info(x)
559-
except Exception as e:
560-
logging.error(f"[ERROR] An error occurred while retrieving information for the '{x}' node.")
561-
logging.error(traceback.format_exc())
562-
return web.json_response(out)
555+
with folder_paths.cache_helper:
556+
out = {}
557+
for x in nodes.NODE_CLASS_MAPPINGS:
558+
try:
559+
out[x] = node_info(x)
560+
except Exception as e:
561+
logging.error(f"[ERROR] An error occurred while retrieving information for the '{x}' node.")
562+
logging.error(traceback.format_exc())
563+
return web.json_response(out)
563564

564565
@routes.get("/object_info/{node_class}")
565566
async def get_object_info_node(request):

0 commit comments

Comments
 (0)