IT技术互动交流平台

OpenResty(nginx)操作memcached的初步应用

作者:haoyunkgq  发布日期:2013-02-20 09:18:11

OpenResty 这里就不介绍了,可以阅读 OpenResty(nginx)操作mysql的初步应用 或 参阅 http://openresty.org


要想在nginx里访问memcached,需要模块 HttpMemcachedModule 或 HttpMemcModule,本文使用的是后者。前者可操作memcached的指令较少,一般用于简单的缓存,并且第一次取数据时需要依赖其他逻辑返回数据才能存储进memcached。而后者可操作memcached的指令较多,灵活,功能比较强大。如果安装OpenResty时没有显示的禁止http_memc_module模块,默认是开启的。OpenResty的安装比较简单,在此略了,可以阅读 OpenResty(nginx)操作mysql的初步应用 里关于OpenResty的安装部分。


1、配置 nginx.conf


worker_processes 1;
events {
        worker_connections 1024;
}
http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        upstream memcached {
                server 127.0.0.1:11211;
        }
        server {
                listen 80;
                server_name localhost;
                root html;
                index index.html index.htm;
                location = /memcached-status {
                        set $memc_cmd stats;
                        memc_pass memcached;
                }
                location / {
                        set $memc_cmd $arg_cmd;
                        set $memc_key $arg_key;
                        set $memc_value $arg_val;
                        set $memc_flags $arg_flags;
                        set $memc_exptime $arg_exptime;

                        memc_cmds_allowed get set add incr delete flush_all;

                        memc_pass memcached;
                }
                error_page 500 502 503 504 /50x.html;
                location = /50x.html {
                        root html;
                }
        }
}

2、测试配置文件,并重启服务

[root@vm5 ~]# /usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@vm5 ~]# killall -HUP nginx

3、测试结果


[root@vm5 conf]# curl 'localhost/?cmd=set&key=1&val=zhangsan'
STORED
[root@vm5 conf]# curl 'localhost/?cmd=get&key=1'
zhangsan
[root@vm5 conf]# curl 'localhost/?cmd=delete&key=1'
DELETED
[root@vm5 conf]# curl 'localhost/?cmd=add&key=2&val=100'
STORED
[root@vm5 conf]# curl 'localhost/?cmd=get&key=2'
100
[root@vm5 conf]# curl 'localhost/?cmd=incr&key=2&val=1'
101
[root@vm5 conf]# curl 'localhost/?cmd=incr&key=2&val=1'
102
[root@vm5 conf]# curl 'localhost/?cmd=decr&key=2&val=1'
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>ngx_openresty/1.2.4.14</center>
</body>
</html>
[root@vm5 conf]# curl 'localhost/?cmd=flush_all'
OK
[root@vm5 conf]# curl 'localhost/?cmd=get&key=1'
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>ngx_openresty/1.2.4.14</center>
</body>
</html>
[root@vm5 conf]# curl 'localhost/?cmd=get&key=2'
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>ngx_openresty/1.2.4.14</center>
</body>
</html>
[root@vm5 conf]# curl 'localhost/memcached-status'
STAT pid 4914
STAT uptime 2774
STAT time 1360198988
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.014997
STAT rusage_system 0.015997
STAT curr_connections 5
STAT total_connections 46
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 35
STAT cmd_set 8
STAT cmd_flush 5
STAT cmd_touch 0
STAT get_hits 11
STAT get_misses 24
STAT delete_misses 0
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 4
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 611
STAT bytes_written 1584
STAT limit_maxbytes 33554432
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 0
STAT curr_items 0
STAT total_items 11
STAT expired_unfetched 1
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 1
END

ok,效果出来了,确实强大!赞一个!
 

Tag标签: OpenResty   memcached  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规