如何用Lua模块高效实现Redis、HTTP、MySQL、Cjson和本地缓存的长尾词查询?

2026-04-01 20:041阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计730个文字,预计阅读时间需要3分钟。

如何用Lua模块高效实现Redis、HTTP、MySQL、Cjson和本地缓存的长尾词查询?

1. Lua模块示例(Redis、HTTP、MySQL、cjson、本地缓存)

1.1. 配置

在nginx.conf中配置: - 设置lua_shared_dict my_cache 128m; - 启用nginx本地缓存,并在http{}块中配置: nginx location /redis-get { resolver 8.8.8.8; default_type text/plain; }

如何用Lua模块高效实现Redis、HTTP、MySQL、Cjson和本地缓存的长尾词查询?

1.4. string工具

local stringEx = {} function stringEx.ToStringEx(value) if type(value)=='table' then return stringEx.TableToStr(value) elseif type(value)=='string' then return "\'"..value.."\'" else return tostring(value) end end function stringEx.TableToStr(t) if t == nil then return "" end local retstr= "{" local i = 1 for key,value in pairs(t) do local signal = "," if i==1 then signal = "" end if key == i then retstr = retstr..signal..stringEx.ToStringEx(value) else if type(key)=='number' or type(key) == 'string' then retstr = retstr..signal..'['..stringEx.ToStringEx(key).."]="..stringEx.ToStringEx(value) else if type(key)=='userdata' then retstr = retstr..signal.."*s"..stringEx.TableToStr(getmetatable(key)).."*e".."="..stringEx.ToStringEx(value) else retstr = retstr..signal..key.."="..stringEx.ToStringEx(value) end end end i = i+1 end retstr = retstr.."}" return retstr end function stringEx.StrToTable(str) if str == nil or type(str) ~= "string" then return end return loadstring("return " .. str)() end return stringEx

1.5. 整合redis+本地缓存

-- 自定义的字符串转换工具 local stringEx = require("stringExt") -- 本地缓存 local local_cache = ngx.shared.my_cache -- redis连接池,设置连接空闲时间 local function close_redis(red) if not red then return end local pool_max_idle_time = 10000 local pool_size = 100 local ok,err = red:set_keepalive(pool_max_idle_time,pool_size) if not ok then ngx.say("set keepalive fail ",err) end end -- 读redis缓存 local function read_redis(key) local redis = require("resty.redis") local red = redis.new(); local ok,err = red:connect("127.0.0.1",8084) if not ok then ngx.say("connect fail ",err) return close_redis(red) end red:set_timeout(1000) local count,err = red:get_reused_times() if 0==count then ok,err = red:auth("123456") if not ok then ngx.say("auth fail ",err) return close_redis(red) end elseif err then ngx.say("fail to get reused times") return close_redis(red) end local res,err = red:get(key) if not res then ngx.say("get msg fail ",err) return close_redis(red) elseif res then ngx.say(" set expire 10000 ") red:expire(key,10) end local_cache:set(key,res,5) ngx.say("read from redis ") ngx.say(res) close_redis(red) end -- github.com/ledgetech/lua-resty-blog.csdn.net/xiejunna/article/details/53445342 local www.xxx.com",{ method="POST", path="/xxx/rpc.api", body = 'a=1&b=2', headers = { ["Content-Type"] = "application/x-www-form-urlencoded", }, keepalive_timeout = 60, keepalive_pool = 10 }) if not res then ngx.say("httpc call fail ") return end local cjson = require("cjson") local json = cjson.new() if not json then ngx.say("json is null") return end -- 测试调用结果 -- ngx.say(stringEx.TableToStr(res)) -- ngx.say(stringEx.ToStringEx(json.decode(res["body"]))) -- ngx.say(type(json.decode(res["body"]))) -- ngx.say(stringEx.ToStringEx(json.decode(res["body"])["header"]["request_seq"])) -- ngx.say(type(json.decode(res["body"])["header"])) -- ngx.say(type(json.decode(res["body"])["header"]["request_seq"])) local connectMysqlUtil = require("connectMysqlUtil") local db = connectMysqlUtil.connect() if not db then ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) return end local res,err,errcode,sqlstate = db:query("select * from t_uls_order_info where order_id='20190119232946000023'",10) if not res then ngx.say("bad request: ",err,":",errcode,": ",sqlstate,".") return end ngx.say("result:",json.encode(res))

1.6. 总结

  1. 本文记录了对http,mysql,redis,nginx本地缓存的基本使用方式,后续需要使用到该模块的需求可以直接参考修改本示例代码
  2. 对于实际的互联网需求,这里可以想象个基于这些模块的需求,优先读取ngnix本地缓存,过期时间较短,其次读取redis缓存,减少redis压力,进一步减少mysql读取压力

本文共计730个文字,预计阅读时间需要3分钟。

如何用Lua模块高效实现Redis、HTTP、MySQL、Cjson和本地缓存的长尾词查询?

1. Lua模块示例(Redis、HTTP、MySQL、cjson、本地缓存)

1.1. 配置

在nginx.conf中配置: - 设置lua_shared_dict my_cache 128m; - 启用nginx本地缓存,并在http{}块中配置: nginx location /redis-get { resolver 8.8.8.8; default_type text/plain; }

如何用Lua模块高效实现Redis、HTTP、MySQL、Cjson和本地缓存的长尾词查询?

1.4. string工具

local stringEx = {} function stringEx.ToStringEx(value) if type(value)=='table' then return stringEx.TableToStr(value) elseif type(value)=='string' then return "\'"..value.."\'" else return tostring(value) end end function stringEx.TableToStr(t) if t == nil then return "" end local retstr= "{" local i = 1 for key,value in pairs(t) do local signal = "," if i==1 then signal = "" end if key == i then retstr = retstr..signal..stringEx.ToStringEx(value) else if type(key)=='number' or type(key) == 'string' then retstr = retstr..signal..'['..stringEx.ToStringEx(key).."]="..stringEx.ToStringEx(value) else if type(key)=='userdata' then retstr = retstr..signal.."*s"..stringEx.TableToStr(getmetatable(key)).."*e".."="..stringEx.ToStringEx(value) else retstr = retstr..signal..key.."="..stringEx.ToStringEx(value) end end end i = i+1 end retstr = retstr.."}" return retstr end function stringEx.StrToTable(str) if str == nil or type(str) ~= "string" then return end return loadstring("return " .. str)() end return stringEx

1.5. 整合redis+本地缓存

-- 自定义的字符串转换工具 local stringEx = require("stringExt") -- 本地缓存 local local_cache = ngx.shared.my_cache -- redis连接池,设置连接空闲时间 local function close_redis(red) if not red then return end local pool_max_idle_time = 10000 local pool_size = 100 local ok,err = red:set_keepalive(pool_max_idle_time,pool_size) if not ok then ngx.say("set keepalive fail ",err) end end -- 读redis缓存 local function read_redis(key) local redis = require("resty.redis") local red = redis.new(); local ok,err = red:connect("127.0.0.1",8084) if not ok then ngx.say("connect fail ",err) return close_redis(red) end red:set_timeout(1000) local count,err = red:get_reused_times() if 0==count then ok,err = red:auth("123456") if not ok then ngx.say("auth fail ",err) return close_redis(red) end elseif err then ngx.say("fail to get reused times") return close_redis(red) end local res,err = red:get(key) if not res then ngx.say("get msg fail ",err) return close_redis(red) elseif res then ngx.say(" set expire 10000 ") red:expire(key,10) end local_cache:set(key,res,5) ngx.say("read from redis ") ngx.say(res) close_redis(red) end -- github.com/ledgetech/lua-resty-blog.csdn.net/xiejunna/article/details/53445342 local www.xxx.com",{ method="POST", path="/xxx/rpc.api", body = 'a=1&b=2', headers = { ["Content-Type"] = "application/x-www-form-urlencoded", }, keepalive_timeout = 60, keepalive_pool = 10 }) if not res then ngx.say("httpc call fail ") return end local cjson = require("cjson") local json = cjson.new() if not json then ngx.say("json is null") return end -- 测试调用结果 -- ngx.say(stringEx.TableToStr(res)) -- ngx.say(stringEx.ToStringEx(json.decode(res["body"]))) -- ngx.say(type(json.decode(res["body"]))) -- ngx.say(stringEx.ToStringEx(json.decode(res["body"])["header"]["request_seq"])) -- ngx.say(type(json.decode(res["body"])["header"])) -- ngx.say(type(json.decode(res["body"])["header"]["request_seq"])) local connectMysqlUtil = require("connectMysqlUtil") local db = connectMysqlUtil.connect() if not db then ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) return end local res,err,errcode,sqlstate = db:query("select * from t_uls_order_info where order_id='20190119232946000023'",10) if not res then ngx.say("bad request: ",err,":",errcode,": ",sqlstate,".") return end ngx.say("result:",json.encode(res))

1.6. 总结

  1. 本文记录了对http,mysql,redis,nginx本地缓存的基本使用方式,后续需要使用到该模块的需求可以直接参考修改本示例代码
  2. 对于实际的互联网需求,这里可以想象个基于这些模块的需求,优先读取ngnix本地缓存,过期时间较短,其次读取redis缓存,减少redis压力,进一步减少mysql读取压力