Skip to content

Commit 5427efd

Browse files
committed
perf(cnr): add cache 7-day expiration logic
* for github star, download counts.
1 parent 9a827e4 commit 5427efd

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

glob/cnr_utils.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import platform
55
from dataclasses import dataclass
6-
from datetime import datetime, timedelta
6+
from datetime import datetime, timedelta, timezone
77
from typing import List
88
from urllib.parse import urlencode
99

@@ -99,27 +99,42 @@ async def _get_cnr_data(sync_mode=None, dont_wait=True, **kwargs):
9999
cached_data = None
100100
last_updated = None
101101
full_nodes = {}
102+
is_cache_expired = True
103+
cache_built_at = None
102104

103105
if normalized_mode != 'force' and manager_util.get_cache_state(uri, expired_days=None) == 'cached':
104106
try:
105107
with open(cache_path, 'r', encoding="UTF-8", errors="ignore") as json_file:
106108
cached_data = json.load(json_file)
107109

108-
if (cached_data.get('comfyui_ver') == comfyui_ver and
110+
cache_built_at = cached_data.get('cache_built_at')
111+
if cache_built_at:
112+
try:
113+
built_dt = datetime.fromisoformat(cache_built_at.replace('Z', '+00:00'))
114+
current_dt = datetime.now(timezone.utc)
115+
delta_dt = current_dt - built_dt
116+
if timedelta(seconds=0) <= delta_dt and delta_dt < timedelta(days=7):
117+
is_cache_expired = False
118+
except Exception:
119+
pass
120+
121+
if not is_cache_expired and (cached_data.get('comfyui_ver') == comfyui_ver and
109122
cached_data.get('form_factor') == form_factor):
110123
last_updated = cached_data.get('last_updated')
111124
for node in cached_data.get('nodes', []):
112125
full_nodes[node['id']] = node
113126
else:
114-
logging.info("[ComfyUI-Manager] Environment change detected. Invalidating local ComfyRegistry cache.")
127+
logging.info("[ComfyUI-Manager] Registry cache expired or environment changed. Invalidating local cache.")
115128
cached_data = None
116129
full_nodes = {}
117130
last_updated = None
131+
is_cache_expired = True
118132
except Exception as e:
119133
logging.error(f"[ComfyUI-Manager] Failed to read cached data: {e}")
120134
cached_data = None
121135
full_nodes = {}
122136
last_updated = None
137+
is_cache_expired = True
123138

124139
if normalized_mode == 'cache':
125140
is_cache_loading = True
@@ -130,7 +145,7 @@ async def _get_cnr_data(sync_mode=None, dont_wait=True, **kwargs):
130145
return cached_data.get('nodes', [])
131146
return []
132147

133-
if cached_data is not None and manager_util.get_cache_state(uri, expired_days=1) == 'cached':
148+
if cached_data is not None and not is_cache_expired:
134149
is_cache_loading = False
135150
return cached_data.get('nodes', [])
136151

@@ -191,11 +206,17 @@ async def fetch_all(timestamp_filter, existing_nodes):
191206
else:
192207
new_timestamp = last_updated
193208

209+
if is_cache_expired or normalized_mode == 'force' or not cache_built_at:
210+
new_cache_built_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
211+
else:
212+
new_cache_built_at = cache_built_at
213+
194214
cache_to_save = {
195215
'nodes': json_obj['nodes'],
196216
'comfyui_ver': comfyui_ver,
197217
'form_factor': form_factor,
198-
'last_updated': new_timestamp
218+
'last_updated': new_timestamp,
219+
'cache_built_at': new_cache_built_at
199220
}
200221
manager_util.save_to_cache(uri, cache_to_save)
201222
return json_obj['nodes']

0 commit comments

Comments
 (0)