Add folder content caching

This commit is contained in:
hayden 2025-01-23 13:53:41 +08:00
parent 82c3afe077
commit d8eae1b241

View File

@ -13,8 +13,19 @@ from typing import Literal
class OutputManager:
def __init__(self) -> None:
self.cache: dict[str, tuple[list, float]] = {}
self.output_uri = folder_paths.get_output_directory()
def get_cache(self, key: str):
return self.cache.get(key, ([], 0))
def set_cache(self, key: str, value: tuple[list, float]):
self.cache[key] = value
def rm_cache(self, key: str):
if key in self.cache:
del self.cache[key]
def add_routes(self, routes) -> None:
@routes.get("/output{pathname:.*}")
async def get_output_file_or_files(request):
@ -53,6 +64,7 @@ class OutputManager:
os.remove(filepath)
elif os.path.isdir(filepath):
shutil.rmtree(filepath)
self.rm_cache(filepath)
return web.Response(status=200)
except Exception as e:
return web.Response(status=500)
@ -61,6 +73,12 @@ class OutputManager:
return f"{self.output_uri}/{pathname}"
def get_folder_items(self, folder: str):
result, m_time = self.get_cache(folder)
folder_m_time = os.path.getmtime(folder)
if folder_m_time == m_time:
return result
result = []
def get_file_info(entry: os.DirEntry[str]):
@ -87,6 +105,7 @@ class OutputManager:
continue
result.append(file_info)
self.set_cache(folder, (result, os.path.getmtime(folder)))
return result
def assert_file_type(self, filename: str, content_types: Literal["image", "video", "audio"]):