openrestry+lua

lua操作mysql:

location /testMysql{
default_type "text/html"; 
content_by_lua_block{
local cjson = require "cjson"
local mysql = require "resty.mysql"

        local db = mysql:new()
        local ok,err = db:connect{
            host="192.168.25.111",
            port=3306,
            user="root",
            password="123456",
            database="changgou"
        }
        db:set_timeout(1000)

--db:send_query("select * from users where id =1")
-- db:send_query("select * from users")
--local res,err,errcode,sqlstate =  db:query("select * from users")
--local res,err,errcode,sqlstate = db:query("insert into users(id,username,birthday,salary) values(null,'zhangsan','2020-11-11',32222.0)")
--local res,err,errcode,sqlstate = db:query("update users set username='lisi' where id = 6")

        local uri_args = ngx.req.get_uri_args()
    name = uri_args['name'] 
    --insertsql = "insert into openrestrytest(name) values("..name..")"
        --selectsql = "select * from openrestrytest where id='2'"
        updatesql = "update openrestrytest set name='dk' where id='4'"
    local res,err,errcode,sqlstate = db:query(updatesql)
        --local res,err,errcode,sqlstate = db:query("select * from tb_user where username='zhangsan'")
        ngx.say(cjson.encode(res))
    db:close()
--local res,err,errcode,sqlstate = db:read_result()
--ngx.say(res[1].id..","..res[1].username..","..res[1].birthday..","..res[1].salary)
}
}

lua操作单机redis

location /testRedis{
  default_type 'text/html';
  content_by_lua_block{
    local redis = require "resty.redis"
    local redisObj = redis:new()
    redisObj:set_timeout(1000)
    local ok,err = redisObj:connect("192.168.25.111",6379)
    if not ok then
      ngx.say("failed to connection redis",err)
      return
    end
    
   local uri_args = ngx.req.get_uri_args()
   name = uri_args['name']
   ok,err = redisObj:set("openrestry:"..name,name)
   if not ok then
    ngx.say("failed to set username",err)
    return 
   end
   local res,err = redisObj:get(name)
   ngx.say(res)
   redisObj:close()
}
}

操作redis集群:

location /rc {
 default_type text/html;
       content_by_lua_file lua/redis-cluster.lua;

}

local config = {
    name = "test",
    serv_list = {
   
        {ip="192.168.25.113", port = 6001},
        {ip="192.168.25.113", port = 6002},
        {ip="192.168.25.113", port = 6003},
        {ip="192.168.25.113", port = 6004},
        {ip="192.168.25.113", port = 6005},
        {ip="192.168.25.113", port = 6006},

 },
    idle_timeout    = 1000,
    pool_size       = 10000,
}

--引入redis集群配置
local redis_cluster = require "resty.rediscluster"

local lredis = {}

--根据key查询
function get(key)
        --创建链接
        local red = redis_cluster:new(config)
        red:init_pipeline()

        --根据key获取数据
        red:get(key)
        local rresult = red:commit_pipeline()

        --关闭链接
        red:close()

        return rresult
end


--添加带过期的数据
function setexp(key,value,time)
        --创建链接
        local red = redis_cluster:new(config)
        red:init_pipeline()

        --添加key,同时设置过期时间
        red:set(key,value)
        red:expire(key,time)

        local rresult = red:commit_pipeline()
end

--根据key查询hash
function hget(key1,key2)
        --创建链接
        local red = redis_cluster:new(config)
        red:init_pipeline()

        --根据key获取数据
        red:hmget(key1,key2)
        local rresult = red:commit_pipeline()

        --关闭链接
        red:close()

        return rresult[1]
end

--hash数据添加
function hset(key1,key2,value)
        --创建链接
        local red = redis_cluster:new(config)
        red:init_pipeline()

        --添加hash数据
        red:hmset(key1,key2,value)
        local rresult = red:commit_pipeline()

        --关闭链接
        red:close()
        return rresult
end

--hash中指定的key自增
function hincrby(key1,key2,value)
        --创建链接
        local red = redis_cluster:new(config)
        red:init_pipeline()

        --添加hash数据
        red:hincrby(key1,key2,value)
        local rresult = red:commit_pipeline()

        --关闭链接
        red:close()

        return rresult
end
--hset("SKU_S1235433012716498944","num",111)

--return lredis
setexp("sex","nan",60)
local aa =get("sex")[1]
ngx.say("aa"..aa)
 

nginx+lua:实现三级缓存

local cjson = require("cjson")
local redis = require "resty.redis"
--调用new方法,获取 redis对象
local red = redis:new()

--先从本地缓存获取,如果没有的话则从redis获取,如果没有直接调用后台服务查询数据库
function get_from_cache(key)
   --ngx.log(ngx.ERR,"请求的phone:"..key)
   local cache_ngx = ngx.shared.ngx_cache
   --先从本地缓存获取
   local value = cache_ngx:get(key)
   if not value then
      --ngx.log(ngx.ERR,"本地缓存为空...")
      local rev,err = get_to_redis("personinifo:"..key)
      if rev == ngx.null then
         --ngx.log(ngx.ERR,"redis缓存为空...")
         --调用应用查询数据库
         local res =ngx.location.capture("/hm/qh/getCustInfo",{ method = ngx.HTTP_POST,args = obj})
         personinfo = res.body
         set_to_redis("pers:"..key,personinfo)
         return personinfo
      else
         -- 添加缓存到内存字典
         --ngx.log(ngx.ERR,"redis缓存不为空直接还回...")
         set_to_cache(key,rev,60)
         return rev
      end
   else
      --ngx.log(ngx.ERR,"本地缓存不为空直接还回...")
      return value
   end
end

-- 向 redis 添加缓存数据
function set_to_redis(key,value)
   -- 设置 redis 超时时间
   red:set_timeout(10000)
   -- 连接 redis 服务器
   local ok,err = red:connect("192.168.25.111",6379)
   local ok,err = red:set(key,value)
   red:expire("personinifo:"..key,2*60)
end

-- 从 redis 获取缓存数据
function get_to_redis(key)
   -- 设置 redis 超时时间
   red:set_timeout(10000)
   -- 连接 redis 服务器
   local ok,err = red:connect("192.168.25.111",6379)
   -- 从 redis 获取缓存数据
   local res,err = red:get(key)
   return res
end

function set_to_cache(key,value,exptime)
   if not exptime then
   exptime = 0
   end
   local cache_ngx = ngx.shared.ngx_cache
   local succ,err,forrcible = cache_ngx:set(key,value,exptime)
   return succ
end

ngx.req.read_body()
local jsonBody = ngx.req.get_body_data()
local obj = cjson.decode(jsonBody)
local phone = obj.data.phone
local person_info = get_from_cache("phone_"..phone)
ngx.say(person_info)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值