使用nginx+lua腳本讀寫redis緩存(一)

架構圖php

日常咱們使用緩存都是在後端的tomcat中進行判斷,是否去查redis,在這個架構裏面是使用nginx寫lua腳本直接去redis中拿數據,這樣的話會幫助tomcat減小不少請求。html

安裝nginx

下載OpenRestyredis

http://openresty.org/cn/download.html,下載windows版本,解壓便可windows

配置後端

根據書中推薦的項目結構,創建以下的項目結構緩存

其中example文件夾是項目目錄,名稱可定爲項目路徑。lua目錄下放該項目用到的lua腳本。lualib目錄中放該項目用到的lua庫。example.conf做爲項目的配置文件。tomcat

編輯C:\work\openresty-1.13.6.1-win32\conf目錄下的nginx.conf文件bash

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
	lua_package_path "C:\work\openresty-1.13.6.1-win32\example\lualib/?.lua;;";  #lua 模塊  
	lua_package_cpath "C:\work\openresty-1.13.6.1-win32\example\lualib/?.so;;";
    include       mime.types;
    include C:\work\openresty-1.13.6.1-win32\example\example.conf;#引入項目的配置文件

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

example配置文件中編輯服務器

server {
    listen       80;  
    server_name  _;  
  
    location /lua {
        default_type 'text/html';  
        lua_code_cache on;  
        content_by_lua_file C:\work\openresty-1.13.6.1-win32\example\lua/test.lua;  
    }

    location /lua_redis_basic {  
	    default_type 'text/html';  
	    lua_code_cache off;  
	    content_by_lua_file C:\work\openresty-1.13.6.1-win32\example\lua/test_redis_basic.lua;  
	}
}

若是訪問的是/lua路徑,交由example/lua/test.lua文件進行處理

若是訪問的是/lua_redis_basic路徑交由example/lua/test_redis_basic.lua文件處理

新建test.lua

ngx.say("hello world");

簡單的輸出「hello world」

新建test_redis_basic.lua文件

local function close_redis(red)  
    if not red then  
        return  
    end  
    local ok, err = red:close()  
    if not ok then  
        ngx.say("close redis error : ", err)  
    end  
end  
  
local redis = require("resty.redis")  
  
--建立實例  
local red = redis:new()  
--設置超時(毫秒)  
red:set_timeout(1000)  
--創建鏈接  
local ip = "127.0.0.1"  
local port = 6379  
local ok, err = red:connect(ip, port)  
if not ok then  
    ngx.say("connect to redis error : ", err)  
    return close_redis(red)  
end 

local res, err = red:auth("shiyuesoft")
if not res then
    ngx.say("failed to authenticate: ", err)
    return
end


--調用API進行處理  
ok, err = red:set("msg", "hello world")  
if not ok then  
    ngx.say("set msg error : ", err)  
    return close_redis(red)  
end  
  
--調用API獲取數據  
local resp, err = red:get("msg")  
if not resp then  
    ngx.say("get msg error : ", err)  
    return close_redis(red)  
end  
--獲得的數據爲空處理  
if resp == ngx.null then  
    resp = ''  --好比默認值  
end  
ngx.say("msg : ", resp)  
  
close_redis(red)

至此配置完成,啓動nginx

訪問127.0.0.1/lua顯示以下

訪問127.0.0.1/lua_redis_basic

至此完成了nginx+lua讀取redis緩存的功能,下一章進行回源到服務器進行操做,並更新redis緩存操做

相關文章
相關標籤/搜索