srcache_nginx redis 构建缓存系统应用一例

redis是一种高效的key-value存储。srcache_nginx模块相关参数介绍,可以参见《memc_nginx+srcache_nginx+memcached构建透明的动态页面缓存》。

下面举一例应用,看配置:

upstream redis {
server 127.0.0.1:6380;
keepalive 512;
}server {
listen       80 backlog=1024 default;
server_name  www.ttlsa.com;
index index.html index.htm index.php;
root  /data/wwwroot/www.ttlsa.com/webroot;

location / {
set $flag 0;
if ($uri ~ /thumb/[0-9]+_160.jpg$){
set $flag "${flag}1";
}
if ($arg_unitid = 42012){
set $flag "${flag}1";
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?kohana_uri=$1 last;
}
}
location ~ .*\.php?$ {
srcache_store_private on;
srcache_methods GET;
srcache_response_cache_control off;
if ($flag = "011"){
set $key $request_uri;
set_escape_uri $escaped_key $key;
srcache_fetch GET /redis $key;
srcache_default_expire 172800;
srcache_store PUT /redis2 key=$escaped_key&exptime=$srcache_expire;

add_header X-flag $flag;
add_header X-Cached-From $srcache_fetch_status;
add_header X-Cached-Store $srcache_store_status;
add_header X-Key $key;
set_md5 $md5key $key;
add_header X-md5-key $md5key;
add_header X-Query_String $query_string;
add_header X-expire $srcache_expire;
}
include fastcgi_params;
fastcgi_pass  127.0.0.1:10080;
fastcgi_index index.php;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
}

location = /redis {
internal;
set_md5 $redis_key $args;
redis_pass redis;
}

location = /redis2 {
internal;

set_unescape_uri $exptime $arg_exptime;
set_unescape_uri $key $arg_key;
set_md5 $key;

redis2_query set $key $echo_request_body;
redis2_query expire $key $exptime;
redis2_pass redis;
}
}

测试:memcached

redis实例下:

memcached

可以记录下日志来测试加缓存前后的耗时。日志格式如下:

log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent $request_time $bytes_sent $request_length '
'[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

转载请注明来自运维生存时间: http://www.ttlsa.com/html/3952.html

One thought on “srcache_nginx redis 构建缓存系统应用一例

发表评论