nginx/src/http/ngx_http_cache.c

97 lines
2.2 KiB
C
Raw Normal View History

2003-05-29 21:02:09 +08:00
2003-06-02 23:24:30 +08:00
/*
* small file in malloc()ed memory, mmap()ed file, file descriptor only,
2003-06-16 02:32:13 +08:00
* file access time only (to estimate could pages still be in memory),
2003-06-02 23:24:30 +08:00
* translated URI (ngx_http_index_hanlder),
* compiled script (ngx_http_ssi_filter).
*/
2003-05-29 21:02:09 +08:00
#define NGX_HTTP_CACHE_ENTRY_DELETED 0x00000001
#define NGX_HTTP_CACHE_ENTRY_MMAPED 0x00000002
/* "/" -> "/index.html" in ngx_http_index_handler */
#define NGX_HTTP_CACHE_ENTRY_URI 0x00000004
2003-06-16 02:32:13 +08:00
/* 301 location "/dir" -> "dir/" in ngx_http_core_handler */
/* compiled script in ngx_http_ssi_filter */
2003-06-02 23:24:30 +08:00
#define NGX_HTTP_CACHE_ENTRY_SCRIPT 0x00000008
2003-05-29 21:02:09 +08:00
#define NGX_HTTP_CACHE_FILTER_FLAGS 0xFFFF0000
typedef struct {
ngx_fd_t fd;
off_t size;
void *data;
time_t accessed;
time_t last_modified;
time_t updated; /* no needed with kqueue */
int refs;
int flags;
} ngx_http_cache_entry_t;
typedef struct {
u_int32_t crc;
ngx_str_t uri;
ngx_http_cache_t *cache;
} ngx_http_cache_hash_entry_t;
typedef struct {
ngx_http_cache_t *cache;
u_int32_t crc;
int n;
} ngx_http_cache_handle_t;
int ngx_http_cache_get(ngx_http_cache_hash_t *cache_hash,
ngx_str_t *uri, ngx_http_cache_handle_t *h)
{
int hi;
ngx_http_cache_hash_entry_t *entry;
2003-06-02 23:24:30 +08:00
h->crc = ngx_crc(uri->data, uri->len);
2003-05-29 21:02:09 +08:00
hi = h->crc % cache_hash->size;
entry = cache_hash[hi].elts;
for (i = 0; i < cache_hash[hi].nelts; i++) {
if (entry[i].crc == crc
&& entry[i].uri.len == uri->len
&& ngx_strncmp(entry[i].uri.data, uri->data, uri->len) == 0
{
h->cache = entry[i].cache;
h->cache->refs++;
h->n = hi;
return NGX_OK;
}
}
return NGX_ERROR;
}
2003-06-02 23:24:30 +08:00
/* 32-bit crc16 */
int ngx_crc(char *data, size_t len)
{
u_int32_t sum;
for (sum = 0; len; len--) {
/*
2003-07-01 23:00:03 +08:00
* gcc 2.95.2 x86 and icc 7.1.006 compile that operator
* into the single rol opcode.
2003-06-02 23:24:30 +08:00
* msvc 6.0sp2 compiles it into four opcodes.
*/
sum = sum >> 1 | sum << 31;
sum += *data++;
}
return sum;
}