src/http/ngx_http_cache.h - nginx-1.7.10

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_HTTP_CACHE_H_INCLUDED_
  6. #define _NGX_HTTP_CACHE_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_http.h>


  10. #define NGX_HTTP_CACHE_MISS          1
  11. #define NGX_HTTP_CACHE_BYPASS        2
  12. #define NGX_HTTP_CACHE_EXPIRED       3
  13. #define NGX_HTTP_CACHE_STALE         4
  14. #define NGX_HTTP_CACHE_UPDATING      5
  15. #define NGX_HTTP_CACHE_REVALIDATED   6
  16. #define NGX_HTTP_CACHE_HIT           7
  17. #define NGX_HTTP_CACHE_SCARCE        8

  18. #define NGX_HTTP_CACHE_KEY_LEN       16
  19. #define NGX_HTTP_CACHE_ETAG_LEN      42
  20. #define NGX_HTTP_CACHE_VARY_LEN      42

  21. #define NGX_HTTP_CACHE_VERSION       3


  22. typedef struct {
  23.     ngx_uint_t                       status;
  24.     time_t                           valid;
  25. } ngx_http_cache_valid_t;


  26. typedef struct {
  27.     ngx_rbtree_node_t                node;
  28.     ngx_queue_t                      queue;

  29.     u_char                           key[NGX_HTTP_CACHE_KEY_LEN
  30.                                          - sizeof(ngx_rbtree_key_t)];

  31.     unsigned                         count:20;
  32.     unsigned                         uses:10;
  33.     unsigned                         valid_msec:10;
  34.     unsigned                         error:10;
  35.     unsigned                         exists:1;
  36.     unsigned                         updating:1;
  37.     unsigned                         deleting:1;
  38.                                      /* 11 unused bits */

  39.     ngx_file_uniq_t                  uniq;
  40.     time_t                           expire;
  41.     time_t                           valid_sec;
  42.     size_t                           body_start;
  43.     off_t                            fs_size;
  44.     ngx_msec_t                       lock_time;
  45. } ngx_http_file_cache_node_t;


  46. struct ngx_http_cache_s {
  47.     ngx_file_t                       file;
  48.     ngx_array_t                      keys;
  49.     uint32_t                         crc32;
  50.     u_char                           key[NGX_HTTP_CACHE_KEY_LEN];
  51.     u_char                           main[NGX_HTTP_CACHE_KEY_LEN];

  52.     ngx_file_uniq_t                  uniq;
  53.     time_t                           valid_sec;
  54.     time_t                           last_modified;
  55.     time_t                           date;

  56.     ngx_str_t                        etag;
  57.     ngx_str_t                        vary;
  58.     u_char                           variant[NGX_HTTP_CACHE_KEY_LEN];

  59.     size_t                           header_start;
  60.     size_t                           body_start;
  61.     off_t                            length;
  62.     off_t                            fs_size;

  63.     ngx_uint_t                       min_uses;
  64.     ngx_uint_t                       error;
  65.     ngx_uint_t                       valid_msec;

  66.     ngx_buf_t                       *buf;

  67.     ngx_http_file_cache_t           *file_cache;
  68.     ngx_http_file_cache_node_t      *node;

  69.     ngx_msec_t                       lock_timeout;
  70.     ngx_msec_t                       lock_age;
  71.     ngx_msec_t                       lock_time;
  72.     ngx_msec_t                       wait_time;

  73.     ngx_event_t                      wait_event;

  74.     unsigned                         lock:1;
  75.     unsigned                         waiting:1;

  76.     unsigned                         updated:1;
  77.     unsigned                         updating:1;
  78.     unsigned                         exists:1;
  79.     unsigned                         temp_file:1;
  80.     unsigned                         reading:1;
  81.     unsigned                         secondary:1;
  82. };


  83. typedef struct {
  84.     ngx_uint_t                       version;
  85.     time_t                           valid_sec;
  86.     time_t                           last_modified;
  87.     time_t                           date;
  88.     uint32_t                         crc32;
  89.     u_short                          valid_msec;
  90.     u_short                          header_start;
  91.     u_short                          body_start;
  92.     u_char                           etag_len;
  93.     u_char                           etag[NGX_HTTP_CACHE_ETAG_LEN];
  94.     u_char                           vary_len;
  95.     u_char                           vary[NGX_HTTP_CACHE_VARY_LEN];
  96.     u_char                           variant[NGX_HTTP_CACHE_KEY_LEN];
  97. } ngx_http_file_cache_header_t;


  98. typedef struct {
  99.     ngx_rbtree_t                     rbtree;
  100.     ngx_rbtree_node_t                sentinel;
  101.     ngx_queue_t                      queue;
  102.     ngx_atomic_t                     cold;
  103.     ngx_atomic_t                     loading;
  104.     off_t                            size;
  105. } ngx_http_file_cache_sh_t;


  106. struct ngx_http_file_cache_s {
  107.     ngx_http_file_cache_sh_t        *sh;
  108.     ngx_slab_pool_t                 *shpool;

  109.     ngx_path_t                      *path;
  110.     ngx_path_t                      *temp_path;

  111.     off_t                            max_size;
  112.     size_t                           bsize;

  113.     time_t                           inactive;

  114.     ngx_uint_t                       files;
  115.     ngx_uint_t                       loader_files;
  116.     ngx_msec_t                       last;
  117.     ngx_msec_t                       loader_sleep;
  118.     ngx_msec_t                       loader_threshold;

  119.     ngx_shm_zone_t                  *shm_zone;
  120. };


  121. ngx_int_t ngx_http_file_cache_new(ngx_http_request_t *r);
  122. ngx_int_t ngx_http_file_cache_create(ngx_http_request_t *r);
  123. void ngx_http_file_cache_create_key(ngx_http_request_t *r);
  124. ngx_int_t ngx_http_file_cache_open(ngx_http_request_t *r);
  125. ngx_int_t ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf);
  126. void ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf);
  127. void ngx_http_file_cache_update_header(ngx_http_request_t *r);
  128. ngx_int_t ngx_http_cache_send(ngx_http_request_t *);
  129. void ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf);
  130. time_t ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status);

  131. char *ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  132.     void *conf);
  133. char *ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  134.     void *conf);


  135. extern ngx_str_t  ngx_http_cache_status[];


  136. #endif /* _NGX_HTTP_CACHE_H_INCLUDED_ */