博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx+Lua 积累
阅读量:6661 次
发布时间:2019-06-25

本文共 1866 字,大约阅读时间需要 6 分钟。

1、解析16进制编码的中文参数

local encodeStr = "%E6%B0%94"local decodeStr = "";for i = 2, #encodeStr - 1, 3 do    local num = encodeStr:sub(i, i + 1);    num = tonumber(num, 16);    decodeStr = decodeStr .. string.char(num);endngx.say(decodeStr)

2、类似replace

local str = "a1b1c1d"local result = string.gsub(str,"1","2")   --将1替换成2 local str = "A1B1C1" local result = string.gsub(str,"1","0",2)    --输出的结果为:A0B0C1

3、直连mysql

local mysql = require "resty.mysql"local db = mysql:new()db:connect{    host = "10.10.3.218",    port = 3306,    database = "test_db",    user = "root",    password = "123456",    max_packet_size = 1024*1024}local result = db:query("SELECT ID,NAME FROM TABLE")ngx.say(result[1]["ID"])ngx.say(result[1]["NAME"])

4、直接Redis

local redis = require "resty.redis"local cache = redis.new()cache.connect(cache,"10.10.3.208", "6379")local result = cache:get("key")

5、使用管道

local redis = require "resty.redis"local cache = redis.new()cache.connect(cache,"10.10.3.208", "6379")cache:init_pipeline()for i=1,10 do    cache:get("key")endlocal res = cache:commit_pipeline()for j=1,#res do    ngx.say(res[j])end

6、计算一共有多少页

local totalPage = math.floor((totalRow+pageSize-1)/pageSize)

 7、Lua Table 多字段排序

--排列顺序优先级从高到低依次为:--第一:等级由高到低;--第二:稀有度由高到低;--第三:伙伴ID从高到低。local function sort_(a, b)    local r    local al = tonumber(a.level)    local bl = tonumber(b.level)    local aq = tonumber(a.data.quality)    local bq = tonumber(b.data.quality)    local aid = tonumber(a.pid)    local bid = tonumber(b.pid)    if  al == bl then        if aq == bq then            r = aid > bid        else            r = aq > bq        end     else        r = al > bl    end    return rendtable.sort(tableName,sort_)

 8、四舍五入小数点保留2位

local function keepTwoDecimalPlaces(decimal)        decimal = math.floor((decimal * 100)+0.5)*0.01               return  decimal endngx.say(keepTwoDecimalPlaces(1.369))

 

 

转载地址:http://gwxto.baihongyu.com/

你可能感兴趣的文章
WCF学习2
查看>>
阵列无法解挂导致VCS双机倒换失败
查看>>
ORACLE中用for in 使用cursor
查看>>
Apache - AH00451
查看>>
vim使用技巧
查看>>
ImportError: No module named extern;
查看>>
su身份切换梳理
查看>>
C# Parellel.For 和 Parallel.ForEach
查看>>
【题解】 CF949C Data Center Maintenance
查看>>
命令类型即使用帮助
查看>>
Cannot determine embedded database driver class for database type NONE
查看>>
asyncio 学习
查看>>
vs2010的一个opencv插件
查看>>
OGRE里mesh和submesh的关系
查看>>
Layer中自定义属性的动画
查看>>
网络传输基础
查看>>
BZOJ-1225-[HNOI2001] 求正整数
查看>>
第2章 shell的语法
查看>>
read和readFully的区别! .
查看>>
Git合并分支或者冲突
查看>>