OpenResty(nginx)操做memcached的初步應用

OpenResty 這裏就不介紹了,能夠閱讀  OpenResty(nginx)操做mysql的初步應用 或 參閱 http://openresty.org

要想在nginx裏訪問memcached,須要模塊 HttpMemcachedModule 或 HttpMemcModule,本文使用的是後者。前者可操做memcached的指令較少,通常用於簡單的緩存,而且第一次取數據時須要依賴其餘邏輯返回數據才能存儲進memcached。然後者可操做memcached的指令較多,靈活,功能比較強大。若是安裝 OpenResty時沒有顯示的禁止http_memc_module模塊,默認是開啓的。 OpenResty的安裝比較簡單,在此略了,能夠閱讀  OpenResty(nginx)操做mysql的初步應用 裏關於 OpenResty的安裝部分。另外也能夠經過nginx模塊HttpLuaModule的lua-resty-memcached庫來操做。

方法一
經過 HttpMemcModule模塊

一、配置 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;
                }
        }
}

二、測試配置文件,並重啓服務
[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

三、測試結果
[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

四、也能夠根據LM(Last Modified)實現304緩存
a、修改 nginx 配置文件,在 location / 段里加入下面指令
memc_flags_to_last_modified on;
b、測試並重啓 nginx
[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
c、測試
[root@vm5 conf]# curl -i 'localhost/?cmd=get&key=1'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Thu, 07 Feb 2013 09:25:03 GMT
Content-Type: text/plain
Content-Length: 2
Last-Modified: Thu, 07 Feb 2013 01:22:26 GMT
Connection: keep-alive

22
看到了吧,在 http 頭裏加入了 Last-Modified,沒開啓 memc_flags_to_last_modified 時是沒有這個http頭的。
咱們再加頭訪問一次
[root@vm5 conf]# curl -H 'If-Modified-Since: Thu, 07 Feb 2013 01:22:26 GMT' -i 'localhost/?cmd=get&key=1'
HTTP/1.1
304 Not Modified
Server: ngx_openresty/1.2.4.14
Date: Thu, 07 Feb 2013 09:29:45 GMT
Last-Modified: Thu, 07 Feb 2013 01:22:26 GMT
Connection: keep-alive

咱們修改剛纔請求的 key 值
[root@vm5 conf]# date +%s
1360229729
[root@vm5 conf]# curl -i 'localhost/?cmd=set&key=1&val=33&flags= 1360229729'
HTTP/1.1 201 Created
Server: ngx_openresty/1.2.4.14
Date: Thu, 07 Feb 2013 09:35:47 GMT
Content-Type: text/plain
Content-Length: 8
Connection: keep-alive

STORED
咱們再請求一次
[root@vm5 conf]# curl -H 'If-Modified-Since: Thu, 07 Feb 2013 01:22:26 GMT' -i 'localhost/?cmd=get&key=1'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Thu, 07 Feb 2013 09:37:51 GMT
Content-Type: text/plain
Content-Length: 2
Last-Modified: Thu, 07 Feb 2013 09:35:29 GMT
Connection: keep-alive

33
此次仍然使用的是老的時間戳,服務器端對比客戶端發送的時間戳和從memcached獲取的時間戳,發現不一致,就發送新內容到客戶端,這裏咱們獲得了新值 33

使用這個特性能夠下降沒必要要的網絡流量消耗,節省成本。其實,服務器是將咱們傳遞的flags參數設置到了memcached的set指令對應的flag字段,即:
key flag expire length
set 1 1360229729 0 2

方法二
經過 HttpLuaModule 模塊 的lua-resty-memcached庫

一、配置 nginx.conf
worker_processes 1;
error_log logs/error.log debug;
events {
        worker_connections 1024;
}
http {
        include mime.types;
        default_type application/octet-stream;
        #default_type 'text/plain';
        sendfile on;
        keepalive_timeout 65;
        server {
                listen 80;
                server_name localhost;
                root html;
                index index.html index.htm;
                lua_code_cache on;
                location / {
                        content_by_lua_file conf/lua/memcached.lua;
                }
                error_page 500 502 503 504 /50x.html;
                location = /50x.html {
                        root html;
                }
        }
}
其中 conf/lua/memcached.lua 代碼以下:
local cmd = tostring(ngx.var.arg_cmd)
local key = tostring(ngx.var.arg_key)
local val = tostring(ngx.var.arg_val)
local flags = tostring(ngx.var.arg_flags or 0)
local exptime = tostring(ngx.var.arg_exptime or 0)

local commands = {
        set="set",
        get="get",
        gets="gets",
        add="add",
        delete="delete",
        flush_all="flush_all",
        incr="incr",
        decr="decr",
        replace="replace",
        append="append",
        prepend="prepend",
        stats="stats",
        version="version"
}
cmd = commands[cmd]
if not cmd then ngx.exit(400) end

local memcached = require("resty.memcached")
local memc,err = memcached:new()

if not memc then
        ngx.say("failed to instantiate memc: ",err)
        return
end

memc:set_timeout(1000)

local ok,err = memc:connect("127.0.0.1",11211)
if not ok then
        ngx.say("failed to connect: ",err)
        return
end

if cmd == "get" then
        if not key then ngx.exit(400) end
        local res,flags,err = memc:get(key)
        if err then
                ngx.say("failed to get ",key," : ",err)
                return
        end
        if not res then
                ngx.exit(404)
        end
        ngx.say(res)
end

if cmd == "set" then
        if not (key and val) then ngx.exit(400) end
        local ok,err = memc:set(key,val,exptime,flags)
        if not ok then
                ngx.say("failed to set ",key," : ",err)
                return
        end
        ngx.say("STORED")
end

二、測試並重啓 nginx
三、測試
[root@vm5 conf]# curl -i 'localhost/?cmd=set&key=1&val=this'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Thu, 07 Feb 2013 17:22:21 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

STORED
[root@vm5 conf]# curl -i 'localhost/?cmd=get&key=1'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Thu, 07 Feb 2013 17:22:30 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

this

ok,效果出來了,確實強大!贊一個!
相關文章
相關標籤/搜索