speedup extra networks listing

This commit is contained in:
AUTOMATIC1111
2023-07-16 09:25:32 +03:00
parent a1d6ada69a
commit 47d9dd0240
8 changed files with 51 additions and 38 deletions

View File

@@ -1,12 +1,12 @@
import json
import os.path
import filelock
import threading
from modules.paths import data_path, script_path
cache_filename = os.path.join(data_path, "cache.json")
cache_data = None
cache_lock = threading.Lock()
def dump_cache():
@@ -14,7 +14,7 @@ def dump_cache():
Saves all cache data to a file.
"""
with filelock.FileLock(f"{cache_filename}.lock"):
with cache_lock:
with open(cache_filename, "w", encoding="utf8") as file:
json.dump(cache_data, file, indent=4)
@@ -33,17 +33,18 @@ def cache(subsection):
global cache_data
if cache_data is None:
with filelock.FileLock(f"{cache_filename}.lock"):
if not os.path.isfile(cache_filename):
cache_data = {}
else:
try:
with open(cache_filename, "r", encoding="utf8") as file:
cache_data = json.load(file)
except Exception:
os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
with cache_lock:
if cache_data is None:
if not os.path.isfile(cache_filename):
cache_data = {}
else:
try:
with open(cache_filename, "r", encoding="utf8") as file:
cache_data = json.load(file)
except Exception:
os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
cache_data = {}
s = cache_data.get(subsection, {})
cache_data[subsection] = s