nginx proxy_cache 下的文件内存索引(fcn)和文件的一些实现

来源:互联网 发布:手机变平板软件 编辑:程序博客网 时间:2024/04/28 16:29
指令:
proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time];


demo:
proxy_cache_path /home/disk2/czh_autohome levels=1:2 keys_zone=czh_autohome_c2:512m inactive=1d max_size=512m;


inactive:
Cached data that are not accessed during the time specified by the inactive parameter get removed from the cache regardless of their freshness. By default, inactive is set to 10 minutes.

max_size:
The special “cache manager” process monitors the maximum cache size set by the max_size parameter. When this size is exceeded, it removes the least recently used data.


文件缓存节点fcn(代表proxy_cache在内存中的索引对象),在源代码中,涉及inactive的地方


一、正常请求到来时:
在方法ngx_http_file_cache_exists内部:

//当结束时,在当前时间的基础上,增加inactive的值
done:
fcn->expire = ngx_time() + cache->inactive;


二、cache loader 加载缓存文件时


在方法ngx_http_file_cache_add内部:


//在通过遍历目录加载指定文件后,设置fcn属性,并插入队列

fcn->expire = ngx_time() +cache->inactive;
ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);


三、cache manager 管理缓存过期时

在方法ngx_http_file_cache_manager内部:

//先删除过期的缓存文件
ngx_http_file_cache_expire

//如果缓存文件的总容量超过设置的max_size容量,则强制删除过期文件
if (size <cache->max_size) {
return next;
}

//执行强制过期

wait = ngx_http_file_cache_forced_expire(cache);




在方法ngx_http_file_cache_expire内部:

//计算缓存文件索引是否已经过期
wait = fcn->expire - now;

if (wait > 0) {
wait = wait > 10 ? 10 : wait;
break;

}

//如果该缓存文件节点已经过期,并且缓存节点的引用计数为0,则执行删除

        if (fcn->count == 0) {
            ngx_http_file_cache_delete(cache, q, name);
            continue;
        }

在 ngx_http_file_cache_delete内部

1、如果文件缓存节点存在(fcn->exists),则根据文件路径,删除文件
2、如果文件缓存节点引用计数(fcn->count)为0,则从队列中删除该节点

0 0