comparison src/http/ngx_http_file_cache.c @ 5959:f7584d7c0ccb

Cache: update variant while setting header. Some parts of code related to handling variants of a resource moved into a separate function that is called earlier. This allows to use cache file name as a prefix for temporary file in the following patch.
author Valentin Bartenev <vbart@nginx.com>
date Fri, 26 Dec 2014 16:22:56 +0300
parents 610832763648
children e9effef98874
comparison
equal deleted inserted replaced
5958:a9138c35120d 5959:f7584d7c0ccb
35 size_t len, u_char *hash); 35 size_t len, u_char *hash);
36 static void ngx_http_file_cache_vary_header(ngx_http_request_t *r, 36 static void ngx_http_file_cache_vary_header(ngx_http_request_t *r,
37 ngx_md5_t *md5, ngx_str_t *name); 37 ngx_md5_t *md5, ngx_str_t *name);
38 static ngx_int_t ngx_http_file_cache_reopen(ngx_http_request_t *r, 38 static ngx_int_t ngx_http_file_cache_reopen(ngx_http_request_t *r,
39 ngx_http_cache_t *c); 39 ngx_http_cache_t *c);
40 static ngx_int_t ngx_http_file_cache_update_variant(ngx_http_request_t *r,
41 ngx_http_cache_t *c);
40 static void ngx_http_file_cache_cleanup(void *data); 42 static void ngx_http_file_cache_cleanup(void *data);
41 static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache); 43 static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache);
42 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache); 44 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
43 static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache, 45 static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache,
44 ngx_queue_t *q, u_char *name); 46 ngx_queue_t *q, u_char *name);
1120 1122
1121 return ngx_http_file_cache_open(r); 1123 return ngx_http_file_cache_open(r);
1122 } 1124 }
1123 1125
1124 1126
1125 void 1127 ngx_int_t
1126 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf) 1128 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
1127 { 1129 {
1128 ngx_http_file_cache_header_t *h = (ngx_http_file_cache_header_t *) buf; 1130 ngx_http_file_cache_header_t *h = (ngx_http_file_cache_header_t *) buf;
1129 1131
1130 u_char *p; 1132 u_char *p;
1162 h->vary_len = (u_char) c->vary.len; 1164 h->vary_len = (u_char) c->vary.len;
1163 ngx_memcpy(h->vary, c->vary.data, c->vary.len); 1165 ngx_memcpy(h->vary, c->vary.data, c->vary.len);
1164 1166
1165 ngx_http_file_cache_vary(r, c->vary.data, c->vary.len, c->variant); 1167 ngx_http_file_cache_vary(r, c->vary.data, c->vary.len, c->variant);
1166 ngx_memcpy(h->variant, c->variant, NGX_HTTP_CACHE_KEY_LEN); 1168 ngx_memcpy(h->variant, c->variant, NGX_HTTP_CACHE_KEY_LEN);
1167 1169 }
1168 } else { 1170
1169 ngx_memzero(c->variant, NGX_HTTP_CACHE_KEY_LEN); 1171 if (ngx_http_file_cache_update_variant(r, c) != NGX_OK) {
1172 return NGX_ERROR;
1170 } 1173 }
1171 1174
1172 p = buf + sizeof(ngx_http_file_cache_header_t); 1175 p = buf + sizeof(ngx_http_file_cache_header_t);
1173 1176
1174 p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key)); 1177 p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
1177 for (i = 0; i < c->keys.nelts; i++) { 1180 for (i = 0; i < c->keys.nelts; i++) {
1178 p = ngx_copy(p, key[i].data, key[i].len); 1181 p = ngx_copy(p, key[i].data, key[i].len);
1179 } 1182 }
1180 1183
1181 *p = LF; 1184 *p = LF;
1185
1186 return NGX_OK;
1187 }
1188
1189
1190 static ngx_int_t
1191 ngx_http_file_cache_update_variant(ngx_http_request_t *r, ngx_http_cache_t *c)
1192 {
1193 ngx_http_file_cache_t *cache;
1194
1195 if (!c->secondary) {
1196 return NGX_OK;
1197 }
1198
1199 if (c->vary.len
1200 && ngx_memcmp(c->variant, c->key, NGX_HTTP_CACHE_KEY_LEN) == 0)
1201 {
1202 return NGX_OK;
1203 }
1204
1205 /*
1206 * if the variant hash doesn't match one we used as a secondary
1207 * cache key, switch back to the original key
1208 */
1209
1210 cache = c->file_cache;
1211
1212 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1213 "http file cache main key");
1214
1215 ngx_shmtx_lock(&cache->shpool->mutex);
1216
1217 c->node->count--;
1218 c->node->updating = 0;
1219 c->node = NULL;
1220
1221 ngx_shmtx_unlock(&cache->shpool->mutex);
1222
1223 c->file.name.len = 0;
1224
1225 ngx_memcpy(c->key, c->main, NGX_HTTP_CACHE_KEY_LEN);
1226
1227 if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
1228 return NGX_ERROR;
1229 }
1230
1231 if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
1232 return NGX_ERROR;
1233 }
1234
1235 return NGX_OK;
1182 } 1236 }
1183 1237
1184 1238
1185 void 1239 void
1186 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf) 1240 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
1201 1255
1202 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1256 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1203 "http file cache update"); 1257 "http file cache update");
1204 1258
1205 cache = c->file_cache; 1259 cache = c->file_cache;
1206
1207 if (c->secondary
1208 && ngx_memcmp(c->variant, c->key, NGX_HTTP_CACHE_KEY_LEN) != 0)
1209 {
1210 /*
1211 * if the variant hash doesn't match one we used as a secondary
1212 * cache key, switch back to the original key
1213 */
1214
1215 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1216 "http file cache main key");
1217
1218 ngx_shmtx_lock(&cache->shpool->mutex);
1219
1220 c->node->count--;
1221 c->node->updating = 0;
1222 c->node = NULL;
1223
1224 ngx_shmtx_unlock(&cache->shpool->mutex);
1225
1226 c->file.name.len = 0;
1227
1228 ngx_memcpy(c->key, c->main, NGX_HTTP_CACHE_KEY_LEN);
1229
1230 if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
1231 return;
1232 }
1233
1234 if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
1235 return;
1236 }
1237 }
1238 1260
1239 c->updated = 1; 1261 c->updated = 1;
1240 c->updating = 0; 1262 c->updating = 0;
1241 1263
1242 uniq = 0; 1264 uniq = 0;