Skip to content

Commit b59eb64

Browse files
committed
perf(cnr): add cache 30-days expiration logic (again)
1 parent 5513395 commit b59eb64

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

glob/cnr_utils.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
lock = asyncio.Lock()
2020

2121
is_cache_loading = False
22+
force_refresh_days = 30
2223

2324
async def get_cnr_data(sync_mode=None, dont_wait=True, **kwargs):
2425
# For backwards compatibility with keyword argument cache_mode
@@ -100,29 +101,44 @@ async def _get_cnr_data(sync_mode=None, dont_wait=True, **kwargs):
100101
cache_built_at = None
101102
cache_created_at = None
102103

104+
# Load local cache
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+
# Check `force_refresh_days` cache database expiration for a full refresh sync
111+
is_db_expired = True
112+
cache_created_at = cached_data.get('cache_created_at')
113+
if cache_created_at:
114+
try:
115+
created_dt = datetime.fromisoformat(cache_created_at.replace('Z', '+00:00'))
116+
current_dt = datetime.now(timezone.utc)
117+
delta_created = current_dt - created_dt
118+
if timedelta(seconds=0) <= delta_created and delta_created < timedelta(days=force_refresh_days):
119+
is_db_expired = False
120+
except Exception:
121+
pass
122+
123+
if not is_db_expired and (cached_data.get('comfyui_ver') == comfyui_ver and
109124
cached_data.get('form_factor') == form_factor):
110125
last_updated = cached_data.get('last_updated')
111-
cache_created_at = cached_data.get('cache_created_at')
112126
for node in cached_data.get('nodes', []):
113127
full_nodes[node['id']] = node
114128
else:
115-
logging.info("[ComfyUI-Manager] Environment change detected. Invalidating local ComfyRegistry cache.")
129+
logging.info(f"[ComfyUI-Manager] Registry cache DB expired ({force_refresh_days} days) or environment changed. Invalidating local cache.")
116130
cached_data = None
117131
full_nodes = {}
118132
last_updated = None
133+
cache_created_at = None
119134
except Exception as e:
120135
logging.error(f"[ComfyUI-Manager] Failed to read cached data: {e}")
121136
cached_data = None
122137
full_nodes = {}
123138
last_updated = None
124139

125140
# Separate cache expiration check (1-day period)
141+
# It just determines cache is expired, not cache is exist.
126142
if cached_data is not None:
127143
cache_built_at = cached_data.get('cache_built_at')
128144
if cache_built_at:
@@ -135,19 +151,19 @@ async def _get_cnr_data(sync_mode=None, dont_wait=True, **kwargs):
135151
except Exception:
136152
pass
137153

154+
# Return Cached data when mode is 'cache'
138155
if normalized_mode == 'cache':
139156
is_cache_loading = True
140157

141-
if dont_wait:
158+
if dont_wait and cached_data is not None:
142159
is_cache_loading = False
143-
if cached_data is not None:
144-
return cached_data.get('nodes', [])
145-
return []
160+
return cached_data.get('nodes', [])
146161

147162
if cached_data is not None and (sync_mode == 'local' or not is_cache_expired):
148163
is_cache_loading = False
149164
return cached_data.get('nodes', [])
150165

166+
# Fetch CNR
151167
async def fetch_all(timestamp_filter, existing_nodes):
152168
remained = True
153169
page = 1
@@ -207,10 +223,8 @@ async def fetch_all(timestamp_filter, existing_nodes):
207223
else:
208224
new_timestamp = last_updated
209225

210-
if is_cache_expired or normalized_mode == 'force' or not cache_built_at:
211-
new_cache_built_at = datetime.now(timezone.utc).strftime(timestamp_format)
212-
else:
213-
new_cache_built_at = cache_built_at
226+
227+
new_cache_built_at = datetime.now(timezone.utc).strftime(timestamp_format)
214228

215229
if normalized_mode == 'force' or not cache_created_at:
216230
new_cache_created_at = datetime.now(timezone.utc).strftime(timestamp_format)

0 commit comments

Comments
 (0)