#include <ngx_config.h>
#include <ngx_event.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <math.h>
#include <openssl/md5.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <pthread.h>
#ifndef MAP_NOCORE
#define MAP_NOCORE 0 /* XXX Linux */
#endif
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0 /* XXX Linux */
#endif
#define EXIST 0
#define NOT_EXIST 1
#define MAX_FILE_NUM 8
ngx_int_t ret = 0;
#define LENTH_NGX_INT sizeof(ngx_int_t)
#define ATOMIC_INIT(i) { (i) }
typedef struct {
volatile int counter;
} atomic_t;
ngx_int_t today;
ngx_int_t delete_flag = 0;
#define atomic_read(v) ((v)->counter)
#define atomic_set(v,i) (((v)->counter) = (i))
#define read_lock(fd, offset, whence, len) \
ncache_http_proxy_lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len)
#define readw_lock(fd, offset, whence, len) \
ncache_http_proxy_lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len)
#define write_lock(fd, offset, whence, len) \
ncache_http_proxy_lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)
#define writew_lock(fd, offset, whence, len) \
ncache_http_proxy_lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len)
#define un_lock(fd, offset, whence, len) \
ncache_http_proxy_lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len)
#define is_read_lockable(fd, offset, whence, len) \
lock_test(fd, F_RDLCK, offset, whence, len)
#define is_write_lockable(fd, offset, whence, len) \
lock_test(fd, F_WRLCK, offset, whence, len)
typedef struct {
ngx_int_t file_size;
ngx_int_t page_num;
ngx_int_t page_num_pre;
}ngx_http_ncache_ctx_t;
//from config file, save the cache_dir info
typedef struct{
ngx_str_t cache_dir_path;
ngx_int_t cache_dir_size;
}ncache_http_cache_dir_conf_t;
typedef struct{
u_char buf[4096];
}Page_info;
Page_info *page_start = NULL;
typedef struct{
ngx_int_t page_num;
ngx_int_t file_num;
ngx_int_t data_len;
ngx_int_t next;
}Store_Page;
typedef struct{
ngx_int_t free_page_first;
ngx_int_t free_page_last;
Page_info *page_start;
Store_Page *store_page_info;
ngx_int_t used_nums;
ngx_int_t total_nums;
ngx_int_t file_num;
u_char file_name[128];
ngx_int_t file_name_len;
}Store_Head_Info;
Store_Head_Info *store_file_head = NULL;
Store_Head_Info *glob_store_file[MAX_FILE_NUM];
static ngx_int_t store_file_fd[MAX_FILE_NUM];
static ngx_int_t file_used;
#pragma pack(1)
typedef struct {
u_char key[13]; //md5
time_t exp_time;
unsigned age:16;
unsigned file_size:32;
unsigned refcount:32;
unsigned charset:1;//utf8
unsigned cached:1; //in use
unsigned caching:1;
unsigned zip:1; // gzip
unsigned next:25; //list info
ngx_int_t file_head;
}ngx_http_ncache_hash_t;
#pragma pack()
typedef struct {
ngx_int_t free_index;
atomic_t request_count;
atomic_t shoot_count;
atomic_t time_count;
atomic_t mem_count;
atomic_t ncache_store_files;
atomic_t ncache_store_size;
atomic_t ncache_http_purgerequests;
atomic_t ncache_http_purgehits;
atomic_t ncache_disk_reads;
atomic_t ncache_disk_writes;
atomic_t ncache_http_kbytes_in;
atomic_t ncache_http_kbytes_out;
atomic_t ncache_http_upstream_count;
ngx_uint_t storage;
ngx_uint_t delete_file_nums;
ngx_uint_t delete_file_storage;
}ngx_http_upstream_cache_hash_count_t;
// ncache hash index first floor size
static ngx_int_t MAX_CACHE_NUM = 16777216; /* 2^24 */
// ncache hash index max size
static ngx_int_t FREE_CACHE_NUM = 16777216; /* 2^25 total */
static ngx_array_t *cache_dirs = NULL;
ngx_http_upstream_cache_hash_count_t *nhuchct;
static ngx_int_t ncache_index_fd = 0;
ngx_http_ncache_hash_t *nhucht = NULL;
static ngx_int_t f_lock_base = 0;
static ngx_int_t cache_size = 0;
static ngx_int_t ignore_client_no_cache = 0;
static ngx_int_t use_delete_process = 0;
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static ngx_int_t TIME_OUT_MAX = 65534; /* 2^16 */
static ngx_int_t ngx_http_ncache_header_filter(
ngx_http_request_t *r);
static ngx_int_t ngx_http_ncache_body_filter(
ngx_http_request_t *r, ngx_chain_t *in);
void ncache_restart();
static __inline__ void atomic_inc(atomic_t *v)
{
__asm__ __volatile__(
NGX_SMP_LOCK "incl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
static __inline__ void atomic_dec(atomic_t *v)
{
__asm__ __volatile__(
NGX_SMP_LOCK "decl %0"
:"=m" (v->counter)
:"m" (v->counter));
}
inline static ngx_int_t ncache_http_proxy_lock_reg(ngx_int_t fd,
ngx_int_t cmd, ngx_int_t type, off_t offset, ngx_int_t whence, off_t len)
{
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
return (fcntl(fd, cmd, &lock));
}
inline void
ncache_http_proxy_mem_shoot_count(ngx_int_t flag)
{
switch(flag)
{
case 0:
if(ngx_time() - atomic_read(&nhuchct->time_count) > 1200)
{
atomic_set(&nhuchct->request_count, 0);
atomic_set(&nhuchct->shoot_count, 0);
atomic_set(&nhuchct->ncache_disk_writes, 0);
atomic_set(&nhuchct->ncache_disk_reads, 0);
atomic_set(&nhuchct->ncache_http_upstream_count, 0);
atomic_set(&nhuchct->time_count, ngx_time());
}
atomic_inc(&nhuchct->request_count);
break;
case 1:
atomic_inc(&nhuchct->mem_count);
atomic_inc(&nhuchct->shoot_count);
break;
case 2:
atomic_inc(&nhuchct->shoot_count);
atomic_inc(&nhuchct->ncache_disk_reads);
break;
case 3:
atomic_inc(&nhuchct->ncache_store_files);
break;
case 4:
atomic_inc(&nhuchct->ncache_http_upstream_count);
break;
case -1:
atomic_dec(&nhuchct->ncache_store_files);
break;
}
}
inline static ngx_int_t
ncache_http_proxy_get_request_path(ngx_http_request_t *r)
{
ngx_int_t request_path_len = 0;
char *doman_start = NULL;
char *doman_end = NULL;
ngx_int_t doman_len = 0;
//if you use the squidclient to purge, the request will not have the "host" header
if(r->headers_in.host == NULL)
{
if(r->request_line.data == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log,
0, "r->request_line.data == NULL");
return NGX_ERROR;
}
doman_start = strstr((const char *)(r->request_line.data),
(const char *)("http://"));
if(doman_start)
{
doman_start += 7;
}
if(doman_start == NULL)doman_start = (char *)r->request_line.data;
doman_end = strchr(doman_start, '/');
if(doman_end == NULL)
{
doman_len = r->request_line.len;
}
else
{
doman_len = strchr(doman_start, ' ') - doman_start;
}
request_path_len = doman_len + r->unparsed_uri.len;
}
else
{
request_path_len = r->headers_in.host->value.len + r->unparsed_uri.len;
}
if(r->cache->path == NULL)
{
r->cache->path = ngx_pcalloc(r->pool, sizeof(ngx_path_t));
if(r->cache->path == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log,
0, "r->cache->path ngx_palloc == NULL");
return NGX_ERROR;
}
}
if(r->cache->path->name.data == NULL)
{
r->cache->path->name.data = ngx_palloc(r->pool, request_path_len);
if(r->cache->path->name.data == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log,
0, "request_path alloc == NULL");
return NGX_ERROR;
}
}
if(r->headers_in.host == NULL)
{
ngx_snprintf(r->cache->path->name.data, request_path_len,
"%s%s", doman_start, r->unparsed_uri.data);
}
else
{
ngx_snprintf(r->cache->path->name.data, request_path_len,
"%s%s", r->headers_in.host->value.data, r->unparsed_uri.data);
}
r->cache->path->name.len = request_path_len;
return NGX_OK;
}
inline static ngx_int_t
ncache_http_proxy_get_md5(ngx_http_request_t *r)
{
if(MD5(r->cache->path->name.data, r->cache->path->name.len,
r->cache->md5) == NULL)
{
return NGX_ERROR;
}
return NGX_OK;
}
inline static ngx_int_t
ncache_http_proxy_get_hash_index
没有合适的资源?快使用搜索试试~ 我知道了~
ncache-3.0_64.tar.gz
4星 · 超过85%的资源 需积分: 10 9 下载量 110 浏览量
2009-02-04
14:07:29
上传
评论
收藏 9KB GZ 举报
温馨提示
共2个文件
config:1个
c:1个
ncache-3.0_64.tar.gz安装在nginx下使用
资源推荐
资源详情
资源评论
收起资源包目录
ncache-3.0_64.tar.gz (2个子文件)
ncache
ncache_http_get_cache_module.c 44KB
config 236B
共 2 条
- 1
资源评论
- mget2014-10-09好东西,好用,用上了
small8856
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功