zoukankan      html  css  js  c++  java
  • Nginx+Lua 积累

    Nginx+Lua 积累
    
    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);
    end
    ngx.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")
    end
    local res = cache:commit_pipeline()
    for j=1,#res do
        ngx.say(res[j])
    end
    复制代码
    6、计算一共有多少页
    
    local totalPage = math.floor((totalRow+pageSize-1)/pageSize)
     
  • 相关阅读:
    线段的类
    计算三角形的类
    关于狗的类
    [poj2234] Matches Game
    bzoj[2655] calc
    拉格朗日插值和牛顿插值 菜鸟教程
    NOI模拟赛(3.15) sequence(序列)
    NOI模拟赛(3.13)Hike (远行)
    二分图讲解
    NOI模拟赛(3.8)Problem B
  • 原文地址:https://www.cnblogs.com/archoncap/p/5393470.html
Copyright © 2011-2022 走看看