sliding-window counter for ngx_lua based on redis.
location / {
content_by_lua_block {
local counter = require "resty.counter"
local c = counter.new("test", 2, 5, {{host="127.0.0.1"}})
c:incr(ngx.var.uri)
ngx.print(c:get(ngx.var.uri))
ngx.exit(ngx.HTTP_OK)
}
}
syntax: redis, release = counter.redis_conn(opt)
Get a connected redis object and release
handler.
In case of failures, returns nil
and a string describing the error.
The optional opt
argument is a Lua table holding the following keys:
-
host
the redis host.
-
port
the redis port, defaults is 6379.
-
connect_timeout
the connect timeout(in ms), defaults is 1000.
-
send_timeout
the send timeout(in ms), defaults is 1000.
-
read_timeout
the read timeout(in ms), defaults is 1000.
-
passwd
the redis password.
-
db
the redis db.
-
max_fails
Passive health check, the number of unsuccessful.
-
fail_timeout
Passive health check, the duration of unsuccessful.
syntax: ct = counter.new(name, wind, number, opts)
Creates a counter object.
name
is counter object names.
wind
is sliding-window size. number
is sliding-window number. So, the statistical duration is wind
* number
.
opts
is array of redis cluster opt.
syntax: ct:set_redis_conn_handler(get_x_redis_conn)
Sets the callback function to get the redis connection. defaults is get_hash_redis_conn
.
syntax: ct:incr(key, value?)
The counts incr in key
.
syntax: count, err = ct:get(key)
Gets the count of key
.
syntax: ct:close()
Closes the counter object.
wenqiang li(vislee)
This module is licensed under the GPL license.
Copyright (C) 2022-, by vislee.
All rights reserved.