-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathHls-Line-Number-Total.lua
51 lines (44 loc) · 1.44 KB
/
Hls-Line-Number-Total.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local function close_redis(redis_instance)
if not redis_instance then
return
end
--释放连接(连接池实现)
local pool_max_idle_time = 10000 --毫秒
local pool_size = 100 --连接池大小
local ok, err = redis_instance:set_keepalive(pool_max_idle_time, pool_size)
if not ok then
ngx.say("set keepalive error : ", err)
end
end
-- 接受Nginx传递进来的参数$1 也就是SteamName
local stream_name = ngx.var.stream_name
local total = ngx.var.total
local redis = require("resty.redis");
-- 创建一个redis对象实例。在失败,返回nil和描述错误的字符串的情况下
local redis_instance = redis:new();
--设置后续操作的超时(以毫秒为单位)保护,包括connect方法
redis_instance:set_timeout(1000)
--建立连接
local ip = '127.0.0.1'
local port = 6379
--尝试连接到redis服务器正在侦听的远程主机和端口
local ok,err = redis_instance:connect(ip,port)
if not ok then
ngx.say("connect redis error : ",err)
return err
end
--数据库选择
--redis_instance:select(2)
--调用API获取数据
local resp, err = redis_instance:incr("NUMBERS:"..stream_name)
if not resp then
ngx.say("get msg error : ", err)
return err
end
--得到的数据为空处理
if resp == ngx.null then
ngx.say("this is not redis_data") --比如默认值
return nil
end
ngx.var.total_numbers = resp
close_redis(redis_instance)