11import json
22import re
33import sys
4+ import time
45import urllib .request
56from pathlib import Path
67from 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