Skip to content

vislee/lua-resty-redis-counter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

lua-resty-redis-counter

sliding-window counter for ngx_lua based on redis.

Table of Contents

Synopsis

    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)
        }
    }

Back to TOC

Methods

redis_conn

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.

new

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.

set_redis_conn_handler

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.

incr

syntax: ct:incr(key, value?)

The counts incr in key.

get

syntax: count, err = ct:get(key)

Gets the count of key.

close

syntax: ct:close()

Closes the counter object.

Back to TOC

Author

wenqiang li(vislee)

Back to TOC

Copyright and License

This module is licensed under the GPL license.

Copyright (C) 2022-, by vislee.

All rights reserved.

Back to TOC

About

sliding-window counter for ngx_lua based on redis.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published