• REDIS工具类


    ocal redis = require "resty/redis"
    
    local log = ngx.log
    local ERR = ngx.ERR
    local setmetatable = setmetatable
    
    local _M = {
    }
    
    local mt = { __index = _M }
    
    local function errlog(...)
        log(ERR, "Redis: ", ...)
    end
    
    function _M.exec(self, func)
    
        local red = redis:new()
        red:set_timeout(self.timeout)
    
        local ok, err = red:connect(self.host, self.port)
        if not ok then
            errlog("Cannot connect, host: " .. self.host .. ", port: " .. self.port)
            return nil, err
        end
    
        if self.password ~= '' then
             -- 请注意这里 auth 的调用过程
                local count
                count, err = red:get_reused_times()
                if 0 == count then
                    ok, err = red:auth(self.password)
                    if not ok then
                        ngx.say("failed to auth: ", err)
                        return
                    end
               elseif err then
                    ngx.say("failed to get reused times: ", err)
                    return
              end
        end
    
        red:select(self.database)
    
        local res, err = func(red)
        if res then
            local ok, err = red:set_keepalive(self.max_idle_time, self.pool_size)
            if not ok then
                red:close()
            end
        end
        return res, err
    end
    
    function _M.new(opts)
        local config = opts or {}
        local self = {
            host = config.host or "127.0.0.1",
        password = config.password or '',
            port = config.port or 6379,
            timeout = config.timeout or 5000,
            database = config.database or 0,
            max_idle_time = config.max_idle_time or 60000,
            pool_size = config.pool_size or 100
        }
        return setmetatable(self, mt)
    end
    
    return _M
  • 相关阅读:
    团队项目-Beta冲刺(第一周)
    个人第4次作业—Alpha项目测试
    团队项目——Alpha发布2
    优课堂考勤系统——Alpha发布1
    优课堂—系统设计
    优课堂—需求分析
    thrift学习笔记
    APP分享
    ActionBar
    ListView添加headview
  • 原文地址:https://www.cnblogs.com/justart/p/12023281.html
Copyright © 2020-2023  润新知