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

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

要想在nginx裏訪問redis,須要HttpRedis模塊 或 HttpRedis2Module模塊 或 HttpLuaModule模塊的lua-resty-redis庫。 HttpRedis模塊提供的指令少,功能單一,適合作簡單緩存,可能之後會擴展。 HttpRedis2Module模塊比前面的HttpRedis模塊操做更靈活,功能更強大。最後一個提供了一個操做redis的接口,可根據本身的狀況做一些邏輯處理,相似php開發中的各類擴展,須要本身開發邏輯。若是在安裝OpenResty 時沒有顯示的禁用前兩個模塊,默認是啓用的,對於第三個模塊可使用--with-luajit,開啓luajit支持, lua-resty-redis庫默認是啓用的。

方案1、使用 HttpRedis
一、配置nginx.conf
worker_processes 1;
error_log logs/error.log debug;
events {
        worker_connections 1024;
}
http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        server {
                listen 80;
                server_name localhost;
                root html;
                index index.html index.htm;
                location / {
                        default_type text/html;
                        
set $redis_key $uri;
                        redis_pass 127.0.0.1:6379;
                        error_page 404 = @fetch;

                }
                 location @fetch {
                        root html;
                }

        }
}

二、測試 配置 並重啓nginx
三、測試
[root@vm5 conf]# curl -i localhost/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:14:57 GMT
Content-Type: application/x-javascript
Content-Length: 23
Last-Modified: Wed, 20 Feb 2013 19:08:50 GMT
Connection: keep-alive
Accept-Ranges: bytes

// this a test js file
說明:如今redis緩存裏是空的,什麼都沒存,因此404了,轉向命名location @fetch,從本地文件系統提取/common.js文件,本地是存在這個文件的,因此返回了文件內容「
// this a test js file」,同時返回http狀態碼200。
下面咱們在redis裏存入這個key:
[root@vm5 conf]# redis-cli
redis 127.0.0.1:6379> set '/common.js' '// fetch from redis'
OK
redis 127.0.0.1:6379> keys *
1) "/common.js"
再次請求
[root@vm5 conf]# curl -i localhost/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:23:13 GMT
Content-Type: application/x-javascript
Content-Length: 19
Connection: keep-alive

// fetch from redis
ok,結果如咱們預期

方案2、使用 HttpRedis2Module
一、配置nginx.conf
worker_processes 1;
error_log logs/error.log debug;
events {
        worker_connections 1024;
}
http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        server {
                listen 80;
                server_name localhost;
                root html;
                index index.html index.htm;
                location /get {
                        set_unescape_uri $key $arg_key;
                        redis2_query get $key;
                        redis2_pass 127.0.0.1:6379;
                }
                location /set {
                        set_unescape_uri $key $arg_key;
                        set_unescape_uri $val $arg_val;
                        redis2_query set $key $val;
                        redis2_pass 127.0.0.1:6379;
                }
        }
}

二、測試 配置 並重啓nginx
三、測試
[root@vm5 conf]# curl -i localhost/get?key=/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:49:57 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

$19
// fetch from redis
[root@vm5 conf]# curl -i 'localhost/set?key=/common.js&val=set by nginx'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:50:41 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

+OK
[root@vm5 conf]# curl -i localhost/get?key=/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:50:45 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

$12
set by nginx

ok,結果出來了,其中的$19和$12是返回的數據長度。

方案3、使用 HttpLuaModule模塊的lua-resty-redis庫
一、配置nginx.conf
worker_processes 1;
error_log logs/error.log debug;
events {
        worker_connections 1024;
}
http {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        server {
                listen 80;
                server_name localhost;
                root html;
                index index.html index.htm;
                location / {
                        content_by_lua_file conf/lua/redis.lua;
                }
        }
}
其中conf/lua/redis.lua代碼以下
local cmd = tostring(ngx.var.arg_cmd)
local key = tostring(ngx.var.arg_key)
local val = tostring(ngx.var.arg_val)
local commands = {
        get="get",
        set="set"
}
cmd = commands[cmd]
if not cmd then
        ngx.say("command not found!")
        ngx.exit(400)
end

local redis = require("resty.redis")
local red = redis:new()

red:set_timeout(1000) -- 1 second

local ok,err = red:connect("127.0.0.1",6379)
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,err = red:get(key)
        if not res then
                ngx.say("failed to get ",key,": ",err)
                return
        end
        ngx.say(res)
end

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

二、 測試配置並重啓nginx
三、測試
[root@vm5 conf]# curl -i 'localhost/?cmd=set&key=/common.js&val=set by lua_resty_redis'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 20:20:07 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

OK
[root@vm5 conf]# curl -i 'localhost/?cmd=get&key=/common.js'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 20:20:15 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive

set by lua_resty_redis

ok,結果也是咱們所預期的,大功告成!
最後這個方案比較靈活,要想在線上使用,須要編寫處理邏輯!
相關文章
相關標籤/搜索