---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by hartnett.
--- DateTime: 2019/11/8 11:22
---

local fetch_local_conf = require("apisix.core.config_local").local_conf
local redis_new = require("resty.redis").new
local clone_tab = require("table.clone")

local function new()
    local local_conf, err = fetch_local_conf()
    if not local_conf then
        return nil, err
    end

    local redis_conf = clone_tab(local_conf.redis)
    local redis_host = redis_conf.host or "127.0.0.1"
    local redis_port = redis_conf.port or 6379
    local redis_password = redis_conf.password or ""
    local redis_timeout = redis_conf.timeout or 1000 -- 1 sec

    -- log.warn(string.format("host: %s, port: %s,  password: %s", redis_host,
    --        redis_port,
    --        redis_password
    -- ))

    local red = redis_new()

    red:set_timeouts(redis_timeout, redis_timeout, redis_timeout)

    local ok, err = red:connect(redis_host, redis_port)
    if not ok then
        return nil, err
    end

    local res, err = red:auth(redis_password)
    if not res then
        return nil, err
    end

    return red, err

end

local _M = {
    version = 0.1,
    new = new,
}

return _M
