-- 导入redis local redis = require('resty.redis') -- 初始化redis local red = redis:new() red:set_timeouts(1000, 1000, 1000)
2)封装函数,用来释放Redis连接,其实是放入连接池
1 2 3 4 5 6 7 8 9
-- 关闭redis连接的工具方法,其实是放入连接池 localfunctionclose_redis(red) local pool_max_idle_time = 10000-- 连接的空闲时间,单位是毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx.log(ngx.ERR, "放入redis连接池失败: ", err) end end
-- 导入redis local redis = require('resty.redis') -- 初始化redis local red = redis:new() red:set_timeouts(1000, 1000, 1000)
-- 关闭redis连接的工具方法,其实是放入连接池 localfunctionclose_redis(red) local pool_max_idle_time = 10000-- 连接的空闲时间,单位是毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx.log(ngx.ERR, "放入redis连接池失败: ", err) end end
-- 查询redis的方法 ip和port是redis地址,key是查询的key localfunctionread_redis(ip, port, key) -- 获取一个连接 local ok, err = red:connect(ip, port) ifnot ok then ngx.log(ngx.ERR, "连接redis失败 : ", err) returnnil end -- 查询redis local resp, err = red:get(key) -- 查询失败处理 ifnot resp then ngx.log(ngx.ERR, "查询Redis失败: ", err, ", key = " , key) end --得到的数据为空处理 if resp == ngx.null then resp = nil ngx.log(ngx.ERR, "查询Redis数据为空, key = ", key) end close_redis(red) return resp end
-- 封装函数,发送http请求,并解析响应 localfunctionread_http(path, params) local resp = ngx.location.capture(path,{ method = ngx.HTTP_GET, args = params, }) ifnot resp then -- 记录错误信息,返回404 ngx.log(ngx.ERR, "http查询失败, path: ", path , ", args: ", args) ngx.exit(404) end return resp.body end -- 将方法导出 local _M = { read_http = read_http, read_redis = read_redis } return _M
4.6.2.实现Redis查询
接下来,我们就可以去修改item.lua文件,实现对Redis的查询了。
查询逻辑是:
根据id查询Redis
如果查询失败则继续查询Tomcat
将查询结果返回
1)修改/usr/local/openresty/lua/item.lua文件,添加一个查询函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
-- 导入common函数库 local common = require('common') local read_http = common.read_http local read_redis = common.read_redis -- 封装查询函数 functionread_data(key, path, params) -- 查询本地缓存 local val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end -- 返回数据 return val end
-- 导入common函数库 local common = require('common') local read_http = common.read_http local read_redis = common.read_redis -- 导入cjson库 local cjson = require('cjson')
-- 封装查询函数 functionread_data(key, path, params) -- 查询本地缓存 local val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end -- 返回数据 return val end
-- 导入共享词典,本地缓存 local item_cache = ngx.shared.item_cache
-- 封装查询函数 functionread_data(key, expire, path, params) -- 查询本地缓存 local val = item_cache:get(key) ifnot val then ngx.log(ngx.ERR, "本地缓存查询失败,尝试查询Redis, key: ", key) -- 查询redis val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end end -- 查询成功,把数据写入本地缓存 item_cache:set(key, val, expire) -- 返回数据 return val end
-- 导入common函数库 local common = require('common') local read_http = common.read_http local read_redis = common.read_redis -- 导入cjson库 local cjson = require('cjson') -- 导入共享词典,本地缓存 local item_cache = ngx.shared.item_cache
-- 封装查询函数 functionread_data(key, expire, path, params) -- 查询本地缓存 local val = item_cache:get(key) ifnot val then ngx.log(ngx.ERR, "本地缓存查询失败,尝试查询Redis, key: ", key) -- 查询redis val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end end -- 查询成功,把数据写入本地缓存 item_cache:set(key, val, expire) -- 返回数据 return val end
-- 导入redis local redis = require('resty.redis') -- 初始化redis local red = redis:new() red:set_timeouts(1000, 1000, 1000)
2)封装函数,用来释放Redis连接,其实是放入连接池
1 2 3 4 5 6 7 8 9
-- 关闭redis连接的工具方法,其实是放入连接池 localfunctionclose_redis(red) local pool_max_idle_time = 10000-- 连接的空闲时间,单位是毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx.log(ngx.ERR, "放入redis连接池失败: ", err) end end
-- 导入redis local redis = require('resty.redis') -- 初始化redis local red = redis:new() red:set_timeouts(1000, 1000, 1000)
-- 关闭redis连接的工具方法,其实是放入连接池 localfunctionclose_redis(red) local pool_max_idle_time = 10000-- 连接的空闲时间,单位是毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx.log(ngx.ERR, "放入redis连接池失败: ", err) end end
-- 查询redis的方法 ip和port是redis地址,key是查询的key localfunctionread_redis(ip, port, key) -- 获取一个连接 local ok, err = red:connect(ip, port) ifnot ok then ngx.log(ngx.ERR, "连接redis失败 : ", err) returnnil end -- 查询redis local resp, err = red:get(key) -- 查询失败处理 ifnot resp then ngx.log(ngx.ERR, "查询Redis失败: ", err, ", key = " , key) end --得到的数据为空处理 if resp == ngx.null then resp = nil ngx.log(ngx.ERR, "查询Redis数据为空, key = ", key) end close_redis(red) return resp end
-- 封装函数,发送http请求,并解析响应 localfunctionread_http(path, params) local resp = ngx.location.capture(path,{ method = ngx.HTTP_GET, args = params, }) ifnot resp then -- 记录错误信息,返回404 ngx.log(ngx.ERR, "http查询失败, path: ", path , ", args: ", args) ngx.exit(404) end return resp.body end -- 将方法导出 local _M = { read_http = read_http, read_redis = read_redis } return _M
4.6.2.实现Redis查询
接下来,我们就可以去修改item.lua文件,实现对Redis的查询了。
查询逻辑是:
根据id查询Redis
如果查询失败则继续查询Tomcat
将查询结果返回
1)修改/usr/local/openresty/lua/item.lua文件,添加一个查询函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
-- 导入common函数库 local common = require('common') local read_http = common.read_http local read_redis = common.read_redis -- 封装查询函数 functionread_data(key, path, params) -- 查询本地缓存 local val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end -- 返回数据 return val end
-- 导入common函数库 local common = require('common') local read_http = common.read_http local read_redis = common.read_redis -- 导入cjson库 local cjson = require('cjson')
-- 封装查询函数 functionread_data(key, path, params) -- 查询本地缓存 local val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end -- 返回数据 return val end
-- 导入共享词典,本地缓存 local item_cache = ngx.shared.item_cache
-- 封装查询函数 functionread_data(key, expire, path, params) -- 查询本地缓存 local val = item_cache:get(key) ifnot val then ngx.log(ngx.ERR, "本地缓存查询失败,尝试查询Redis, key: ", key) -- 查询redis val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end end -- 查询成功,把数据写入本地缓存 item_cache:set(key, val, expire) -- 返回数据 return val end
-- 导入common函数库 local common = require('common') local read_http = common.read_http local read_redis = common.read_redis -- 导入cjson库 local cjson = require('cjson') -- 导入共享词典,本地缓存 local item_cache = ngx.shared.item_cache
-- 封装查询函数 functionread_data(key, expire, path, params) -- 查询本地缓存 local val = item_cache:get(key) ifnot val then ngx.log(ngx.ERR, "本地缓存查询失败,尝试查询Redis, key: ", key) -- 查询redis val = read_redis("127.0.0.1", 6379, key) -- 判断查询结果 ifnot val then ngx.log(ngx.ERR, "redis查询失败,尝试查询http, key: ", key) -- redis查询失败,去查询http val = read_http(path, params) end end -- 查询成功,把数据写入本地缓存 item_cache:set(key, val, expire) -- 返回数据 return val end