使用lua從mysql數(shù)據(jù)庫導(dǎo)出數(shù)據(jù)到redis與Nginx緩存

安裝lua

有l(wèi)inux版本的安裝也有mac版本的安裝本冲。准脂。我們采用linux版本的安裝,首先我們準(zhǔn)備一個linux虛擬機(jī)檬洞。

安裝步驟,在linux系統(tǒng)中執(zhí)行下面的命令狸膏。
//下載
curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
//解壓
tar zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make linux test

此時再執(zhí)行l(wèi)ua測試看lua是否安裝成功
[root@localhost ~]# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio</pre>

安裝openresty

linux安裝openresty:

1.添加倉庫執(zhí)行命令

 yum install yum-utils
 yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

2.執(zhí)行安裝

yum install openresty

3.安裝成功后 會在默認(rèn)的目錄如下:

/usr/local/openresty

測試訪問

重啟下centos虛擬機(jī),然后訪問測試Nginx

訪問地址:http://192.168.200.128/

lua腳本從數(shù)據(jù)庫查詢數(shù)據(jù)到redis疮胖,創(chuàng)建update_ad.lua

ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local mysql = require("resty.mysql")
local uri_args = ngx.req.get_uri_args()
//地址傳來的參數(shù)
local position = uri_args["position"]

local db = mysql:new()
db:set_timeout(10000)  
local props = {  
    host = "192.168.200.128",  
    port = 3306,  
    database = "數(shù)據(jù)庫名",  
    user = "root",  
    password = "root"  
}

local res = db:connect(props)  
local select_sql = "select url,image from tb_ad where status ='1' and position='"..position.."' and start_time<= NOW() AND end_time>= NOW()"  
res = db:query(select_sql)  
db:close()  

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

local ip ="192.168.200.128"
local port = 6379
red:connect(ip,port)

red:set("ad_"..position,cjson.encode(res))
red:close()

ngx.say("{flag:true}")

數(shù)據(jù)從redis到nginx,創(chuàng)建read_ad.lua

--設(shè)置響應(yīng)頭類型
ngx.header.content_type="application/json;charset=utf8"
--獲取請求中的參數(shù)ID
local uri_args = ngx.req.get_uri_args();
local position = uri_args["position"];

--獲取本地緩存
local cache_ngx = ngx.shared.dis_cache;
--根據(jù)ID 獲取本地緩存數(shù)據(jù)
local adCache = cache_ngx:get('ad_cache_'..position);

if adCache == "" or adCache == nil then

    --引入redis庫
    local redis = require("resty.redis");
    --創(chuàng)建redis對象
    local red = redis:new()
    --設(shè)置超時時間
    red:set_timeout(2000)
    --連接
    local ok, err = red:connect("192.168.200.128", 6379)
    --獲取key的值
    local rescontent=red:get("ad_"..position)
    --輸出到返回響應(yīng)中
    ngx.say(rescontent)
    --關(guān)閉連接
    red:close()
    --將redis中獲取到的數(shù)據(jù)存入nginx本地緩存
    cache_ngx:set('ad_cache_'..position, rescontent, 10*60);
else
    --nginx本地緩存中獲取到數(shù)據(jù)直接輸出
    ngx.say(adCache)
end


在/usr/local/openresty/nginx/conf/nginx.conf中添加頭信息环戈,和 location信息

server {
    listen       80;
    server_name  localhost;

    location /update_ad {
        content_by_lua_file /root/lua/update_ad.lua;
    }
     location /ad_read {
            #使用限流配置    burst 突發(fā)情況下 允許訪問4個請求
            limit_req zone=adRateLimit burst=4 nodelay;
            content_by_lua_file /usr/local/openresty/lua/read_ad.lua;
        }
}

如果要嘗試訪問可將前端頁面放到/usr/local/openresty/nginx/html
下。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末澎灸,一起剝皮案震驚了整個濱河市院塞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌性昭,老刑警劉巖拦止,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異糜颠,居然都是意外死亡汹族,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門其兴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來顶瞒,“玉大人,你說我怎么就攤上這事元旬×裥欤” “怎么了守问?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長坑资。 經(jīng)常有香客問我耗帕,道長,這世上最難降的妖魔是什么袱贮? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任仿便,我火速辦了婚禮,結(jié)果婚禮上攒巍,老公的妹妹穿的比我還像新娘嗽仪。我一直安慰自己,他們只是感情好柒莉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布钦幔。 她就那樣靜靜地躺著,像睡著了一般常柄。 火紅的嫁衣襯著肌膚如雪鲤氢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天西潘,我揣著相機(jī)與錄音卷玉,去河邊找鬼。 笑死喷市,一個胖子當(dāng)著我的面吹牛相种,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播品姓,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼寝并,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了腹备?” 一聲冷哼從身側(cè)響起衬潦,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎植酥,沒想到半個月后镀岛,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡友驮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年漂羊,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卸留。...
    茶點(diǎn)故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡走越,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出耻瑟,到底是詐尸還是另有隱情旨指,我是刑警寧澤捻悯,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站淤毛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏算柳。R本人自食惡果不足惜低淡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瞬项。 院中可真熱鬧蔗蹋,春花似錦、人聲如沸囱淋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽妥衣。三九已至皂吮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間税手,已是汗流浹背蜂筹。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留芦倒,地道東北人艺挪。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像兵扬,于是被迫代替她去往敵國和親麻裳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容