@@ -126,10 +126,6 @@ ngx_http_cache_pilot_index_header_filter(ngx_http_request_t *r) {
126126 return NGX_ERROR ;
127127 }
128128
129- if (tags -> nelts == 0 ) {
130- goto done ;
131- }
132-
133129 ctx = ngx_http_get_module_ctx (r , ngx_http_cache_pilot_module );
134130 if (ctx == NULL ) {
135131 ctx = ngx_pcalloc (r -> pool , sizeof (* ctx ));
@@ -154,9 +150,10 @@ ngx_http_cache_pilot_index_header_filter(ngx_http_request_t *r) {
154150 * Log phase handler: commit the SHM index entry for a freshly written cache
155151 * file. Runs after the response is sent; the SHM write is in-memory and fast.
156152 *
157- * We only proceed if the request context was stashed in the header filter,
158- * which happens only for cacheable upstream responses - so we naturally skip
159- * cache hits, purge requests, and any request that did not write a cache file.
153+ * The header filter normally stashes the request context for cacheable upstream
154+ * responses. Some no-tag cache writes can reach log phase without that
155+ * context, so the log handler can lazily recover the index zone and empty tag
156+ * set from the request before writing metadata.
160157 *
161158 * If the upstream response turned out to be non-cacheable despite the initial
162159 * header verdict (e.g. a downstream filter cleared cacheable), we silently
@@ -165,20 +162,50 @@ ngx_http_cache_pilot_index_header_filter(ngx_http_request_t *r) {
165162static ngx_int_t
166163ngx_http_cache_pilot_index_log_handler (ngx_http_request_t * r ) {
167164#if (NGX_LINUX )
165+ ngx_http_cache_pilot_loc_conf_t * cplcf ;
168166 ngx_http_cache_pilot_request_ctx_t * ctx ;
169167 ngx_http_cache_index_store_t * writer ;
168+ ngx_http_cache_index_zone_t * zone ;
169+ ngx_array_t * tags ;
170170 ngx_str_t cache_key ;
171171 ngx_str_t * key_parts ;
172+ ngx_str_t zone_name ;
172173 size_t key_len ;
173174 ngx_uint_t i ;
174175
175176 ctx = ngx_http_get_module_ctx (r , ngx_http_cache_pilot_module );
176- if (ctx == NULL || ctx -> index_tags == NULL || ctx -> index_tags -> nelts == 0 ) {
177+ if (r -> cache == NULL || r -> cache -> file . name . len == 0 ) {
177178 return NGX_OK ;
178179 }
179180
180- if (r -> cache == NULL || r -> cache -> file .name .len == 0 ) {
181- return NGX_OK ;
181+ tags = NULL ;
182+ ngx_str_null (& zone_name );
183+
184+ if (ctx != NULL && ctx -> index_tags != NULL ) {
185+ tags = ctx -> index_tags ;
186+ zone_name = ctx -> index_zone_name ;
187+ } else {
188+ if (r -> upstream != NULL
189+ && r -> upstream -> cache_status == NGX_HTTP_CACHE_HIT ) {
190+ return NGX_OK ;
191+ }
192+
193+ cplcf = ngx_http_get_module_loc_conf (r , ngx_http_cache_pilot_module );
194+ if (!ngx_http_cache_index_location_enabled (cplcf )) {
195+ return NGX_OK ;
196+ }
197+
198+ zone = ngx_http_cache_index_lookup_zone (r -> cache -> file_cache );
199+ if (zone == NULL ) {
200+ return NGX_OK ;
201+ }
202+
203+ if (ngx_http_cache_pilot_index_response_headers (r , cplcf , & tags )
204+ != NGX_OK ) {
205+ return NGX_OK ;
206+ }
207+
208+ zone_name = zone -> zone_name ;
182209 }
183210
184211 writer = ngx_http_cache_index_store_writer ();
@@ -209,11 +236,11 @@ ngx_http_cache_pilot_index_log_handler(ngx_http_request_t *r) {
209236
210237 ngx_log_debug2 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
211238 "cache_tag index update zone:\"%V\" path:\"%V\"" ,
212- & ctx -> index_zone_name , & r -> cache -> file .name );
239+ & zone_name , & r -> cache -> file .name );
213240
214- if (ngx_http_cache_index_store_upsert_file_meta (writer , & ctx -> index_zone_name ,
241+ if (ngx_http_cache_index_store_upsert_file_meta (writer , & zone_name ,
215242 & r -> cache -> file .name , & cache_key ,
216- ngx_time (), 0 , ctx -> index_tags , r -> connection -> log ) != NGX_OK ) {
243+ ngx_time (), 0 , tags , r -> connection -> log ) != NGX_OK ) {
217244 ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
218245 "cache_tag index update failed for \"%V\"" ,
219246 & r -> cache -> file .name );
0 commit comments