wandenberg

nginx_cache_with_groups.patch

Dec 5th, 2011
222
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff -rupN /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_fastcgi_module.c /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_fastcgi_module.c
  2. --- /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_fastcgi_module.c    2011-09-30 11:18:58.000000000 -0300
  3. +++ /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_fastcgi_module.c   2011-11-29 13:25:05.024900001 -0200
  4. @@ -28,6 +28,7 @@ typedef struct {
  5.  
  6.  #if (NGX_HTTP_CACHE)
  7.      ngx_http_complex_value_t       cache_key;
  8. +    ngx_http_complex_value_t       cache_group_key;
  9.  #endif
  10.  
  11.  #if (NGX_PCRE)
  12. @@ -317,7 +318,7 @@ static ngx_command_t  ngx_http_fastcgi_c
  13.        NULL },
  14.  
  15.      { ngx_string("fastcgi_cache_key"),
  16. -      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  17. +      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  18.        ngx_http_fastcgi_cache_key,
  19.        NGX_HTTP_LOC_CONF_OFFSET,
  20.        0,
  21. @@ -678,6 +679,17 @@ ngx_http_fastcgi_create_key(ngx_http_req
  22.          return NGX_ERROR;
  23.      }
  24.  
  25. +    if (flcf->cache_group_key.value.len) {
  26. +        key = ngx_array_push(&r->cache->group_keys);
  27. +        if (key == NULL) {
  28. +            return NGX_ERROR;
  29. +        }
  30. +
  31. +        if (ngx_http_complex_value(r, &flcf->cache_group_key, key) != NGX_OK) {
  32. +            return NGX_ERROR;
  33. +        }
  34. +    }
  35. +
  36.      return NGX_OK;
  37.  }
  38.  
  39. @@ -2818,7 +2830,7 @@ ngx_http_fastcgi_cache_key(ngx_conf_t *c
  40.      ngx_http_fastcgi_loc_conf_t *flcf = conf;
  41.  
  42.      ngx_str_t                         *value;
  43. -    ngx_http_compile_complex_value_t   ccv;
  44. +    ngx_http_compile_complex_value_t   ccv, group_ccv;
  45.  
  46.      value = cf->args->elts;
  47.  
  48. @@ -2836,6 +2848,19 @@ ngx_http_fastcgi_cache_key(ngx_conf_t *c
  49.          return NGX_CONF_ERROR;
  50.      }
  51.  
  52. +    if (cf->args->nelts > 2) {
  53. +        /* set proxy_cache_group_key part */
  54. +        ngx_memzero(&group_ccv, sizeof(ngx_http_compile_complex_value_t));
  55. +
  56. +        group_ccv.cf = cf;
  57. +        group_ccv.value = &value[2];
  58. +        group_ccv.complex_value = &flcf->cache_group_key;
  59. +
  60. +        if (ngx_http_compile_complex_value(&group_ccv) != NGX_OK) {
  61. +            return NGX_CONF_ERROR;
  62. +        }
  63. +    }
  64. +
  65.      return NGX_CONF_OK;
  66.  }
  67.  
  68. diff -rupN /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_proxy_module.c /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_proxy_module.c
  69. --- /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_proxy_module.c  2011-09-30 10:42:59.000000000 -0300
  70. +++ /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_proxy_module.c 2011-11-29 13:25:01.614900001 -0200
  71. @@ -65,6 +65,7 @@ typedef struct {
  72.  
  73.  #if (NGX_HTTP_CACHE)
  74.      ngx_http_complex_value_t       cache_key;
  75. +    ngx_http_complex_value_t       cache_group_key;
  76.  #endif
  77.  
  78.      ngx_http_proxy_vars_t          vars;
  79. @@ -326,7 +327,7 @@ static ngx_command_t  ngx_http_proxy_com
  80.        NULL },
  81.  
  82.      { ngx_string("proxy_cache_key"),
  83. -      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  84. +      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  85.        ngx_http_proxy_cache_key,
  86.        NGX_HTTP_LOC_CONF_OFFSET,
  87.        0,
  88. @@ -749,6 +750,17 @@ ngx_http_proxy_create_key(ngx_http_reque
  89.  
  90.      ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
  91.  
  92. +    if (plcf->cache_group_key.value.len) {
  93. +        key = ngx_array_push(&r->cache->group_keys);
  94. +        if (key == NULL) {
  95. +            return NGX_ERROR;
  96. +        }
  97. +
  98. +        if (ngx_http_complex_value(r, &plcf->cache_group_key, key) != NGX_OK) {
  99. +            return NGX_ERROR;
  100. +        }
  101. +    }
  102. +
  103.      key = ngx_array_push(&r->cache->keys);
  104.      if (key == NULL) {
  105.          return NGX_ERROR;
  106. @@ -2689,7 +2701,7 @@ ngx_http_proxy_cache_key(ngx_conf_t *cf,
  107.      ngx_http_proxy_loc_conf_t *plcf = conf;
  108.  
  109.      ngx_str_t                         *value;
  110. -    ngx_http_compile_complex_value_t   ccv;
  111. +    ngx_http_compile_complex_value_t   ccv, group_ccv;
  112.  
  113.      value = cf->args->elts;
  114.  
  115. @@ -2707,6 +2719,19 @@ ngx_http_proxy_cache_key(ngx_conf_t *cf,
  116.          return NGX_CONF_ERROR;
  117.      }
  118.  
  119. +    if (cf->args->nelts > 2) {
  120. +        /* set proxy_cache_group_key part */
  121. +        ngx_memzero(&group_ccv, sizeof(ngx_http_compile_complex_value_t));
  122. +
  123. +        group_ccv.cf = cf;
  124. +        group_ccv.value = &value[2];
  125. +        group_ccv.complex_value = &plcf->cache_group_key;
  126. +
  127. +        if (ngx_http_compile_complex_value(&group_ccv) != NGX_OK) {
  128. +            return NGX_CONF_ERROR;
  129. +        }
  130. +    }
  131. +
  132.      return NGX_CONF_OK;
  133.  }
  134.  
  135. diff -rupN /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_scgi_module.c /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_scgi_module.c
  136. --- /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_scgi_module.c   2011-11-01 12:04:23.000000000 -0200
  137. +++ /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_scgi_module.c  2011-11-28 18:54:19.286253001 -0200
  138. @@ -26,6 +26,7 @@ typedef struct {
  139.  
  140.  #if (NGX_HTTP_CACHE)
  141.      ngx_http_complex_value_t   cache_key;
  142. +    ngx_http_complex_value_t   cache_group_key;
  143.  #endif
  144.  } ngx_http_scgi_loc_conf_t;
  145.  
  146. @@ -190,7 +191,7 @@ static ngx_command_t ngx_http_scgi_comma
  147.        NULL },
  148.  
  149.      { ngx_string("scgi_cache_key"),
  150. -      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  151. +      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  152.        ngx_http_scgi_cache_key,
  153.        NGX_HTTP_LOC_CONF_OFFSET,
  154.        0,
  155. @@ -507,6 +508,17 @@ ngx_http_scgi_create_key(ngx_http_reques
  156.          return NGX_ERROR;
  157.      }
  158.  
  159. +    if (scf->cache_group_key.value.len) {
  160. +        key = ngx_array_push(&r->cache->group_keys);
  161. +        if (key == NULL) {
  162. +            return NGX_ERROR;
  163. +        }
  164. +
  165. +        if (ngx_http_complex_value(r, &scf->cache_group_key, key) != NGX_OK) {
  166. +            return NGX_ERROR;
  167. +        }
  168. +    }
  169. +
  170.      return NGX_OK;
  171.  }
  172.  
  173. @@ -1651,7 +1663,7 @@ ngx_http_scgi_cache_key(ngx_conf_t *cf,
  174.      ngx_http_scgi_loc_conf_t *scf = conf;
  175.  
  176.      ngx_str_t                         *value;
  177. -    ngx_http_compile_complex_value_t   ccv;
  178. +    ngx_http_compile_complex_value_t   ccv, group_ccv;
  179.  
  180.      value = cf->args->elts;
  181.  
  182. @@ -1669,6 +1681,19 @@ ngx_http_scgi_cache_key(ngx_conf_t *cf,
  183.          return NGX_CONF_ERROR;
  184.      }
  185.  
  186. +    if (cf->args->nelts > 2) {
  187. +        /* set proxy_cache_group_key part */
  188. +        ngx_memzero(&group_ccv, sizeof(ngx_http_compile_complex_value_t));
  189. +
  190. +        group_ccv.cf = cf;
  191. +        group_ccv.value = &value[2];
  192. +        group_ccv.complex_value = &scf->cache_group_key;
  193. +
  194. +        if (ngx_http_compile_complex_value(&group_ccv) != NGX_OK) {
  195. +            return NGX_CONF_ERROR;
  196. +        }
  197. +    }
  198. +
  199.      return NGX_CONF_OK;
  200.  }
  201.  
  202. diff -rupN /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_uwsgi_module.c /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_uwsgi_module.c
  203. --- /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/modules/ngx_http_uwsgi_module.c  2011-11-01 12:04:23.000000000 -0200
  204. +++ /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/modules/ngx_http_uwsgi_module.c 2011-11-29 13:24:53.934900001 -0200
  205. @@ -27,6 +27,7 @@ typedef struct {
  206.  
  207.  #if (NGX_HTTP_CACHE)
  208.      ngx_http_complex_value_t   cache_key;
  209. +    ngx_http_complex_value_t   cache_group_key;
  210.  #endif
  211.  
  212.      ngx_str_t                  uwsgi_string;
  213. @@ -217,7 +218,7 @@ static ngx_command_t ngx_http_uwsgi_comm
  214.        NULL },
  215.  
  216.      { ngx_string("uwsgi_cache_key"),
  217. -      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  218. +      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
  219.        ngx_http_uwsgi_cache_key,
  220.        NGX_HTTP_LOC_CONF_OFFSET,
  221.        0,
  222. @@ -540,6 +541,17 @@ ngx_http_uwsgi_create_key(ngx_http_reque
  223.          return NGX_ERROR;
  224.      }
  225.  
  226. +    if (uwcf->cache_group_key.value.len) {
  227. +        key = ngx_array_push(&r->cache->group_keys);
  228. +        if (key == NULL) {
  229. +            return NGX_ERROR;
  230. +        }
  231. +
  232. +        if (ngx_http_complex_value(r, &uwcf->cache_group_key, key) != NGX_OK) {
  233. +            return NGX_ERROR;
  234. +        }
  235. +    }
  236. +
  237.      return NGX_OK;
  238.  }
  239.  
  240. @@ -1703,7 +1715,7 @@ ngx_http_uwsgi_cache_key(ngx_conf_t *cf,
  241.      ngx_http_uwsgi_loc_conf_t *uwcf = conf;
  242.  
  243.      ngx_str_t                         *value;
  244. -    ngx_http_compile_complex_value_t   ccv;
  245. +    ngx_http_compile_complex_value_t   ccv, group_ccv;
  246.  
  247.      value = cf->args->elts;
  248.  
  249. @@ -1721,6 +1733,19 @@ ngx_http_uwsgi_cache_key(ngx_conf_t *cf,
  250.          return NGX_CONF_ERROR;
  251.      }
  252.  
  253. +    if (cf->args->nelts > 2) {
  254. +        /* set proxy_cache_group_key part */
  255. +        ngx_memzero(&group_ccv, sizeof(ngx_http_compile_complex_value_t));
  256. +
  257. +        group_ccv.cf = cf;
  258. +        group_ccv.value = &value[2];
  259. +        group_ccv.complex_value = &uwcf->cache_group_key;
  260. +
  261. +        if (ngx_http_compile_complex_value(&group_ccv) != NGX_OK) {
  262. +            return NGX_CONF_ERROR;
  263. +        }
  264. +    }
  265. +
  266.      return NGX_CONF_OK;
  267.  }
  268.  
  269. diff -rupN /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/ngx_http_cache.h /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/ngx_http_cache.h
  270. --- /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/ngx_http_cache.h 2011-04-22 07:06:43.000000000 -0300
  271. +++ /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/ngx_http_cache.h    2011-11-28 18:33:09.966253003 -0200
  272. @@ -36,6 +36,7 @@ typedef struct {
  273.  
  274.      u_char                           key[NGX_HTTP_CACHE_KEY_LEN
  275.                                           - sizeof(ngx_rbtree_key_t)];
  276. +    u_char                           group_key[NGX_HTTP_CACHE_KEY_LEN];
  277.  
  278.      unsigned                         count:20;
  279.      unsigned                         uses:10;
  280. @@ -57,8 +58,10 @@ typedef struct {
  281.  struct ngx_http_cache_s {
  282.      ngx_file_t                       file;
  283.      ngx_array_t                      keys;
  284. +    ngx_array_t                      group_keys;
  285.      uint32_t                         crc32;
  286.      u_char                           key[NGX_HTTP_CACHE_KEY_LEN];
  287. +    u_char                           group_key[NGX_HTTP_CACHE_KEY_LEN];
  288.  
  289.      ngx_file_uniq_t                  uniq;
  290.      time_t                           valid_sec;
  291. @@ -134,6 +137,7 @@ void ngx_http_file_cache_update(ngx_http
  292.  ngx_int_t ngx_http_cache_send(ngx_http_request_t *);
  293.  void ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf);
  294.  time_t ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status);
  295. +ngx_int_t ngx_http_file_cache_group_clean(ngx_http_file_cache_t  *cache, u_char *group_key);
  296.  
  297.  char *ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  298.      void *conf);
  299. diff -rupN /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/ngx_http_file_cache.c /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/ngx_http_file_cache.c
  300. --- /home/wandenberg/Downloads/nginx/nginx-1.0.10/src/http/ngx_http_file_cache.c    2011-11-01 11:49:31.000000000 -0200
  301. +++ /usr/local/nginxpushstream/source/nginx-1.0.10/src/http/ngx_http_file_cache.c   2011-11-29 17:00:18.314900002 -0200
  302. @@ -4,6 +4,7 @@
  303.   */
  304.  
  305.  
  306. +#include <nginx.h>
  307.  #include <ngx_config.h>
  308.  #include <ngx_core.h>
  309.  #include <ngx_http.h>
  310. @@ -21,6 +22,8 @@ static ngx_int_t ngx_http_file_cache_exi
  311.      ngx_http_cache_t *c);
  312.  static ngx_int_t ngx_http_file_cache_name(ngx_http_request_t *r,
  313.      ngx_path_t *path);
  314. +static ngx_int_t ngx_http_file_cache_fullname(ngx_str_t *fullname,
  315. +    ngx_path_t *path, u_char *key, u_char *group_key);
  316.  static ngx_http_file_cache_node_t *
  317.      ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
  318.  static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
  319. @@ -43,6 +46,12 @@ static ngx_int_t ngx_http_file_cache_add
  320.  static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx,
  321.      ngx_str_t *path);
  322.  
  323. +static ngx_int_t ngx_http_file_cache_purge_file(ngx_tree_ctx_t *ctx,
  324. +    ngx_str_t *path);
  325. +
  326. +static ngx_int_t ngx_http_file_cache_purge_dir(ngx_tree_ctx_t *ctx,
  327. +    ngx_str_t *path);
  328. +
  329.  
  330.  ngx_str_t  ngx_http_cache_status[] = {
  331.      ngx_string("MISS"),
  332. @@ -159,6 +168,10 @@ ngx_http_file_cache_new(ngx_http_request
  333.          return NGX_ERROR;
  334.      }
  335.  
  336. +    if (ngx_array_init(&c->group_keys, r->pool, 4, sizeof(ngx_str_t)) != NGX_OK) {
  337. +        return NGX_ERROR;
  338. +    }
  339. +
  340.      r->cache = c;
  341.      c->file.log = r->connection->log;
  342.      c->file.fd = NGX_INVALID_FILE;
  343. @@ -229,6 +242,24 @@ ngx_http_file_cache_create_key(ngx_http_
  344.  
  345.      ngx_crc32_final(c->crc32);
  346.      ngx_md5_final(c->key, &md5);
  347. +
  348. +    ngx_memset(c->group_key, '\0', NGX_HTTP_CACHE_KEY_LEN);
  349. +    if (c->group_keys.nelts > 0) {
  350. +        ngx_md5_init(&md5);
  351. +
  352. +        key = c->group_keys.elts;
  353. +        for (i = 0; i < c->group_keys.nelts; i++) {
  354. +           ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  355. +                           "http cache group key: \"%V\"", &key[i]);
  356. +
  357. +            len += key[i].len;
  358. +
  359. +            ngx_md5_update(&md5, key[i].data, key[i].len);
  360. +        }
  361. +
  362. +        ngx_md5_final(c->group_key, &md5);
  363. +    }
  364. +
  365.  }
  366.  
  367.  
  368. @@ -610,33 +641,197 @@ failed:
  369.  static ngx_int_t
  370.  ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
  371.  {
  372. -    u_char            *p;
  373. +    ngx_str_t          buf;
  374.      ngx_http_cache_t  *c;
  375.  
  376.      c = r->cache;
  377.  
  378. -    c->file.name.len = path->name.len + 1 + path->len
  379. -                       + 2 * NGX_HTTP_CACHE_KEY_LEN;
  380. +    if (ngx_http_file_cache_fullname(&buf, path, c->key, c->group_key) != NGX_OK) {
  381. +        return NGX_ERROR;
  382. +    }
  383. +
  384. +    c->file.name.len = buf.len;
  385.  
  386.      c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
  387.      if (c->file.name.data == NULL) {
  388.          return NGX_ERROR;
  389.      }
  390.  
  391. -    ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
  392. +    ngx_memcpy(c->file.name.data, buf.data, buf.len);
  393. +    c->file.name.data[c->file.name.len] = '\0';
  394.  
  395. -    p = c->file.name.data + path->name.len + 1 + path->len;
  396. -    p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
  397. +    ngx_free(buf.data);
  398. +
  399. +    return NGX_OK;
  400. +}
  401. +
  402. +
  403. +static ngx_int_t
  404. +ngx_http_file_cache_fullname(ngx_str_t *fullname, ngx_path_t *path, u_char *key, u_char *group_key)
  405. +{
  406. +    u_char            *p, *last;
  407. +    ngx_str_t          buf;
  408. +    size_t             len = 0;
  409. +    ngx_flag_t         has_group_key = 0;
  410. +
  411. +    len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
  412. +
  413. +    buf.len = len;
  414. +    buf.data = ngx_alloc(len + 1, ngx_cycle->log);
  415. +    if (buf.data == NULL) {
  416. +        return NGX_ERROR;
  417. +    }
  418. +
  419. +    if ((group_key != NULL) && (group_key[0] != '\0')) {
  420. +        len += 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
  421. +        has_group_key = 1;
  422. +    }
  423. +
  424. +    fullname->len = len;
  425. +    fullname->data = ngx_alloc(len + 1, ngx_cycle->log);
  426. +
  427. +    if (fullname->data == NULL) {
  428. +        ngx_free(buf.data);
  429. +        return NGX_ERROR;
  430. +    }
  431. +
  432. +    ngx_memcpy(buf.data, path->name.data, path->name.len);
  433. +    last = ngx_copy(fullname->data, path->name.data, path->name.len);
  434. +
  435. +    if (has_group_key) {
  436. +        p = buf.data + path->name.len + 1 + path->len;
  437. +        p = ngx_hex_dump(p, group_key, NGX_HTTP_CACHE_KEY_LEN);
  438. +        *p = '\0';
  439. +
  440. +        ngx_create_hashed_filename(path, buf.data, buf.len);
  441. +        last = ngx_copy(last, buf.data + path->name.len, buf.len - path->name.len);
  442. +    }
  443. +
  444. +    p = buf.data + path->name.len + 1 + path->len;
  445. +    p = ngx_hex_dump(p, key, NGX_HTTP_CACHE_KEY_LEN);
  446.      *p = '\0';
  447.  
  448. -    ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
  449. +    ngx_create_hashed_filename(path, buf.data, buf.len);
  450. +    last = ngx_copy(last, buf.data + path->name.len, buf.len - path->name.len);
  451. +    *last = '\0';
  452.  
  453. -    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  454. -                   "cache file: \"%s\"", c->file.name.data);
  455. +    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  456. +                   "cache file: \"%s\"", fullname->data);
  457. +
  458. +    ngx_free(buf.data);
  459.  
  460.      return NGX_OK;
  461.  }
  462.  
  463. +
  464. +ngx_int_t
  465. +ngx_http_file_cache_group_clean(ngx_http_file_cache_t  *cache, u_char *group_key)
  466. +{
  467. +    ngx_tree_ctx_t  tree;
  468. +    ngx_path_t     *path;
  469. +    ngx_str_t       name;
  470. +    u_char         *p;
  471. +    ngx_int_t       rc;
  472. +    ngx_dir_t       dir;
  473. +
  474. +    if ((group_key == NULL) || (group_key[0] == '\0')) {
  475. +        return NGX_ERROR;
  476. +    }
  477. +
  478. +    path = cache->path;
  479. +    name.len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
  480. +    name.data = ngx_alloc(name.len + 1, ngx_cycle->log);
  481. +    if (name.data == NULL) {
  482. +        return NGX_ERROR;
  483. +    }
  484. +
  485. +    ngx_memcpy(name.data, path->name.data, path->name.len);
  486. +    p = name.data + path->name.len + 1 + path->len;
  487. +    p = ngx_hex_dump(p, group_key, NGX_HTTP_CACHE_KEY_LEN);
  488. +    *p = '\0';
  489. +
  490. +    ngx_create_hashed_filename(path, name.data, name.len);
  491. +
  492. +    /* check if group directory exists */
  493. +    if (ngx_open_dir(&name, &dir) == NGX_ERROR) {
  494. +        return NGX_DECLINED;
  495. +    }
  496. +
  497. +    ngx_close_dir(&dir);
  498. +
  499. +
  500. +    tree.init_handler = NULL;
  501. +    tree.file_handler = ngx_http_file_cache_purge_file;
  502. +    tree.pre_tree_handler = ngx_http_file_cache_noop;
  503. +    tree.post_tree_handler = ngx_http_file_cache_purge_dir;
  504. +    tree.spec_handler = ngx_http_file_cache_delete_file;
  505. +    tree.data = cache;
  506. +    tree.alloc = 0;
  507. +    tree.log = ngx_cycle->log;
  508. +
  509. +    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  510. +                       "cleaning group: \"%s\"", name.data);
  511. +
  512. +    rc = ngx_walk_tree(&tree, &name);
  513. +
  514. +    ngx_http_file_cache_purge_dir(&tree, &name);
  515. +
  516. +    ngx_free(name.data);
  517. +
  518. +    return rc;
  519. +}
  520. +
  521. +
  522. +static ngx_int_t
  523. +ngx_http_file_cache_purge_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  524. +{
  525. +    ngx_http_file_cache_t      *cache;
  526. +    u_char                      key[NGX_HTTP_CACHE_KEY_LEN];
  527. +    ngx_http_file_cache_node_t *fcn;
  528. +
  529. +    cache = ctx->data;
  530. +
  531. +    ngx_shmtx_lock(&cache->shpool->mutex);
  532. +
  533. +    fcn = ngx_http_file_cache_lookup(cache, key);
  534. +
  535. +    if ((fcn != NULL) && fcn->exists) {
  536. +#  if defined(nginx_version) && (nginx_version >= 1000001)
  537. +        cache->sh->size -= fcn->fs_size;
  538. +#  else
  539. +        cache->sh->size -= (fcn->length + cache->bsize - 1) / cache->bsize;
  540. +#  endif
  541. +
  542. +        fcn->exists = 0;
  543. +#  if defined(nginx_version) \
  544. +      && ((nginx_version >= 8001) \
  545. +          || ((nginx_version < 8000) && (nginx_version >= 7060)))
  546. +        fcn->updating = 0;
  547. +#  endif
  548. +
  549. +    }
  550. +
  551. +    ngx_shmtx_unlock(&cache->shpool->mutex);
  552. +
  553. +    ngx_http_file_cache_delete_file(ctx, path);
  554. +
  555. +    return NGX_OK;
  556. +}
  557. +
  558. +
  559. +static ngx_int_t
  560. +ngx_http_file_cache_purge_dir(ngx_tree_ctx_t *ctx, ngx_str_t *path)
  561. +{
  562. +    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
  563. +                           "delete dir: \"%V\"", path);
  564. +
  565. +    if (ngx_delete_dir(path->data) == NGX_FILE_ERROR) {
  566. +        ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
  567. +                          ngx_delete_dir_n " \"%s\" failed", path->data);
  568. +    }
  569. +    return NGX_OK;
  570. +}
  571. +
  572.  
  573.  static ngx_http_file_cache_node_t *
  574.  ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
RAW Paste Data