aboutsummaryrefslogtreecommitdiff
path: root/modules/cache.py
diff options
context:
space:
mode:
authorAUTOMATIC1111 <16777216c@gmail.com>2023-08-05 08:01:38 +0300
committerAUTOMATIC1111 <16777216c@gmail.com>2023-08-05 08:01:38 +0300
commitef1698fd6dbd6387341a1eeeded068ff1476ee50 (patch)
treeddaa0cf76e8cf95b93f63909a026ae3d5eab460a /modules/cache.py
parent0fae47e97445df4e7de4d85538a80917fc2a2457 (diff)
parentc613416af375092f55b9bc8649c949e95d250c44 (diff)
Merge branch 'dev' into extra-networks-always-visible
Diffstat (limited to 'modules/cache.py')
-rw-r--r--modules/cache.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/modules/cache.py b/modules/cache.py
index 28d42a8c..71fe6302 100644
--- a/modules/cache.py
+++ b/modules/cache.py
@@ -1,6 +1,7 @@
import json
import os.path
import threading
+import time
from modules.paths import data_path, script_path
@@ -8,15 +9,37 @@ cache_filename = os.path.join(data_path, "cache.json")
cache_data = None
cache_lock = threading.Lock()
+dump_cache_after = None
+dump_cache_thread = None
+
def dump_cache():
"""
- Saves all cache data to a file.
+ Marks cache for writing to disk. 5 seconds after no one else flags the cache for writing, it is written.
"""
+ global dump_cache_after
+ global dump_cache_thread
+
+ def thread_func():
+ global dump_cache_after
+ global dump_cache_thread
+
+ while dump_cache_after is not None and time.time() < dump_cache_after:
+ time.sleep(1)
+
+ with cache_lock:
+ with open(cache_filename, "w", encoding="utf8") as file:
+ json.dump(cache_data, file, indent=4)
+
+ dump_cache_after = None
+ dump_cache_thread = None
+
with cache_lock:
- with open(cache_filename, "w", encoding="utf8") as file:
- json.dump(cache_data, file, indent=4)
+ dump_cache_after = time.time() + 5
+ if dump_cache_thread is None:
+ dump_cache_thread = threading.Thread(name='cache-writer', target=thread_func)
+ dump_cache_thread.start()
def cache(subsection):
@@ -84,7 +107,7 @@ def cached_data_for_file(subsection, title, filename, func):
if ondisk_mtime > cached_mtime:
entry = None
- if not entry:
+ if not entry or 'value' not in entry:
value = func()
if value is None:
return None