Skip to content

Commit 68a2e4a

Browse files
committed
perf: use cached include analysis for 10 mins before hitting server
1 parent 44da359 commit 68a2e4a

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

include_analysis.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import re
33
import sys
4+
import time
45
import urllib.request
56
from pathlib import Path
67
from typing import Dict, List, Optional, TypedDict
@@ -134,13 +135,20 @@ def get_latest_include_analysis():
134135
cached_file_path = Path(__file__).resolve().parent.joinpath(".cached-include-analysis")
135136
url = "https://commondatastorage.googleapis.com/chromium-browser-clang/include-analysis.js"
136137

138+
cache_max_age_seconds = 10 * 60 # 10 minutes
139+
137140
etag = None
138141
raw_include_analysis = None
139142

140143
if cached_file_path.exists():
141144
with open(cached_file_path, "r") as f:
142145
[etag, raw_include_analysis] = f.read().split("\n", 1)
143146

147+
# If cache is less than 10 minutes old, use it directly
148+
cache_age = time.time() - cached_file_path.stat().st_mtime
149+
if cache_age < cache_max_age_seconds:
150+
return raw_include_analysis
151+
144152
try:
145153
# Make request with ETag if available
146154
request = urllib.request.Request(url)
@@ -158,7 +166,10 @@ def get_latest_include_analysis():
158166
f.write(f"{new_etag}\n{raw_include_analysis}")
159167
except urllib.error.HTTPError as e:
160168
# If not "304 Not Modified", fall back to cache if available, else raise the error
161-
if e.code != 304 and not raw_include_analysis:
169+
if e.code == 304:
170+
# Content unchanged, update mtime so we don't check again for 10 minutes
171+
cached_file_path.touch(exist_ok=True)
172+
elif not raw_include_analysis:
162173
raise
163174
except urllib.error.URLError:
164175
# If there's a network error, fall back to cache if available, else raise the error

0 commit comments

Comments
 (0)