ngx_lua操作Redis和Mysql
生活随笔
收集整理的這篇文章主要介紹了
ngx_lua操作Redis和Mysql
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
功能簡介
- 通過ngx_lua對redis進(jìn)行數(shù)據(jù)的插入和取出
- ngx_lua對mysql的增刪改查
- 瀏覽器以json格式返回數(shù)據(jù)
- 將頻繁調(diào)用的cjson設(shè)置全局,一開始就加載
結(jié)果顯示
redis數(shù)據(jù)插入和取出的顯示:
mysql增刪改查的顯示:
nginx.conf的配置
#設(shè)置全部變量,一開始加載init_by_lua_block{cjson = require "cjson";}server {listen 80;server_name localhost;charset UTF-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /lua{default_type 'text/html';content_by_lua 'ngx.say("<h1>HELLO,OpenRestry</h1>")';}#lua連接redislocation /luaRedis {charset gbk;default_type "text/html";content_by_lua_block{--01 引入redislocal redis=require "resty.redis"--02 創(chuàng)建redis對象local redisObj=redis:new()--03 設(shè)置超時redisObj:set_timeout(1000)--04 連接local ok,errr=redisObj:connect("redis所在的IP",6379)if not ok thenngx.say("連接失敗",err)returnend--05 redis存入數(shù)據(jù)local ok,errr=redisObj:set("name","xiaoTang")if not ok thenngx.say("數(shù)據(jù)導(dǎo)入失敗",err)returnend--06 redis取出數(shù)據(jù)local ans,errr=redisObj:get("name")ngx.say(ans)--07 連接關(guān)閉redisObj:close()}}#lua連接mysqllocation /luaMysql {charset gbk;default_type "text/html";content_by_lua_block{local mysql = require "resty.mysql"--local cjson = require "cjson"local db = mysql:new()local ok,err = db:connect{host="IP",port=3306,user="root",password="密碼",database="nginx_db"}db:set_timeout(1000)db:send_query("select * from users")local res,err,errcode,sqlstate = db:read_result()for i,v in ipairs(res) dongx.say(v.id..","..v.username..","..v.birthday..","..v.salary)end--換行和加粗local html = [[<html><head></head><body><p><b><br />mysql中以json返回數(shù)據(jù)<br /></b></p></body></html>]]ngx.say(html);local jsonDate=cjson.encode(res);ngx.say(jsonDate);--數(shù)據(jù)庫的增刪改查--換行和加粗local html = [[<html><head></head><body><p><b><br />數(shù)據(jù)庫的增刪改查<br /></b></p></body></html>]]ngx.say(html);--mysql的增加--local sql="insert into users(id,username,birthday,salary) values(null,'xiaoTang','1996-04-26',66666.6)";--mysql的刪除--local sql="delete from users where username='xiaoTang'";--mysql的修改--local sql="update users set username='xiaoTang02' where id=3";--mysql的查找local sql="select * from users where username='xiaoTang02'";--數(shù)據(jù)庫執(zhí)行語句直接用querylocal res,err,errcode,sqlstate = db:query(sql);local jsonDate=cjson.encode(res);ngx.say(jsonDate);db:close()}}相關(guān)API介紹
redis的API
lua-resty-redis提供了訪問Redis的詳細(xì)API,包括創(chuàng)建對接、連接、操作、數(shù)據(jù)處理等。這些API基本上與Redis的操作一一對應(yīng)。 (1)redis = require "resty.redis" (2)new語法: redis,err = redis:new(),創(chuàng)建一個Redis對象。 (3)connect語法:ok,err=redis:connect(host,port[,options_table]),設(shè)置連接Redis的連接信息。ok:連接成功返回 1,連接失敗返回nilerr:返回對應(yīng)的錯誤信息 (4)set_timeout語法: redis:set_timeout(time) ,設(shè)置請求操作Redis的超時時間。 (5)close語法: ok,err = redis:close(),關(guān)閉當(dāng)前連接,成功返回1,失敗返回nil和錯誤信息 (6)redis命令對應(yīng)的方法在lua-resty-redis中,所有的Redis命令都有自己的方法,方法名字和命令名字相同,只是全部為小寫。mysql的API
(1)引入"resty.mysql"模塊local mysql = require "resty.mysql" (2)new創(chuàng)建一個MySQL連接對象,遇到錯誤時,db為nil,err為錯誤描述信息語法: db,err = mysql:new() (3)connect嘗試連接到一個MySQL服務(wù)器語法:ok,err=db:connect(options),options是一個參數(shù)的Lua表結(jié)構(gòu),里面包含數(shù)據(jù)庫連接的相關(guān)信息host:服務(wù)器主機名或IP地址port:服務(wù)器監(jiān)聽端口,默認(rèn)為3306user:登錄的用戶名password:登錄密碼database:使用的數(shù)據(jù)庫名 (4)set_timeout設(shè)置子請求的超時時間(ms),包括connect方法語法:db:set_timeout(time) (5)close關(guān)閉當(dāng)前MySQL連接并返回狀態(tài)。如果成功,則返回1;如果出現(xiàn)任何錯誤,則將返回nil和錯誤描述。語法:db:close() (6)send_query異步向遠(yuǎn)程MySQL發(fā)送一個查詢。如果成功則返回成功發(fā)送的字節(jié)數(shù);如果錯誤,則返回nil和錯誤描述語法:bytes,err=db:send_query(sql) (7)read_result從MySQL服務(wù)器返回結(jié)果中讀取一行數(shù)據(jù)。res返回一個描述OK包或結(jié)果集包的Lua表,語法:res, err, errcode, sqlstate = db:read_result() res, err, errcode, sqlstate = db:read_result(rows) :rows指定返回結(jié)果集的最大值,默認(rèn)為4如果是查詢,則返回一個容納多行的數(shù)組。每行是一個數(shù)據(jù)列的key-value對,如總結(jié)
以上是生活随笔為你收集整理的ngx_lua操作Redis和Mysql的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在PyPI上发布自己的python包
- 下一篇: mysql 实现按首字母字典序排序以及根