更新 target/redis/conn.lua
This commit is contained in:
@@ -93,7 +93,35 @@ end
|
||||
function redis_conn:command(cmd)
|
||||
return self.module:command(cmd)
|
||||
end
|
||||
|
||||
--[[
|
||||
执行 Redis 命令
|
||||
@param args 命令参数数组,会自动将参数转换为字符串
|
||||
@return 返回命令执行结果
|
||||
]]
|
||||
function redis_conn:command_ex(args)
|
||||
if args == nil or type(args) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local str_args = {}
|
||||
for i, arg in ipairs(args) do
|
||||
if arg == nil then
|
||||
table.insert(str_args, "")
|
||||
elseif type(arg) == "number" then
|
||||
-- 判断是否为整数:整数不转成小数
|
||||
if arg % 1 == 0 then
|
||||
-- 是整数,使用 %d 格式,避免转成小数
|
||||
table.insert(str_args, string.format("%d", arg))
|
||||
else
|
||||
-- 是浮点数,直接转字符串
|
||||
table.insert(str_args, tostring(arg))
|
||||
end
|
||||
else
|
||||
-- 其他类型(字符串、布尔等)直接转字符串
|
||||
table.insert(str_args, tostring(arg))
|
||||
end
|
||||
end
|
||||
return self.module:command_ex(str_args)
|
||||
end
|
||||
-- ==================== String 操作 ====================
|
||||
|
||||
--[[
|
||||
@@ -114,11 +142,18 @@ end
|
||||
@return 返回执行结果
|
||||
]]
|
||||
function redis_conn:set(key, value, options)
|
||||
local cmd = build_command("SET", key, value)
|
||||
local args = {"SET", key, value}
|
||||
if options then
|
||||
cmd = cmd .. " " .. options
|
||||
-- 如果 options 是字符串,需要拆分(如 "EX 10" 或 "NX")
|
||||
if type(options) == "string" then
|
||||
for word in string.gmatch(options, "%S+") do
|
||||
table.insert(args, word)
|
||||
end
|
||||
else
|
||||
table.insert(args, options)
|
||||
end
|
||||
end
|
||||
return self:command(cmd)
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -129,7 +164,7 @@ end
|
||||
@return 返回执行结果
|
||||
]]
|
||||
function redis_conn:setex(key, seconds, value)
|
||||
return self:command(build_command("SETEX", key, seconds, value))
|
||||
return self:command_ex({"SETEX", key, seconds, value})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -139,7 +174,7 @@ end
|
||||
@return 返回 1 表示设置成功,0 表示键已存在
|
||||
]]
|
||||
function redis_conn:setnx(key, value)
|
||||
return self:command(build_command("SETNX", key, value))
|
||||
return self:command_ex({"SETNX", key, value})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -148,7 +183,8 @@ end
|
||||
@return 返回删除的键数量
|
||||
]]
|
||||
function redis_conn:del(...)
|
||||
return self:command(build_command("DEL", ...))
|
||||
local args = {"DEL", ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -157,7 +193,7 @@ end
|
||||
@return 返回 1 表示存在,0 表示不存在
|
||||
]]
|
||||
function redis_conn:exists(key)
|
||||
return self:command(build_command("EXISTS", key))
|
||||
return self:command_ex({"EXISTS", key})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -167,7 +203,7 @@ end
|
||||
注意:在生产环境中应谨慎使用,建议使用 SCAN 命令替代
|
||||
]]
|
||||
function redis_conn:keys(pattern)
|
||||
local result = self:command(build_command("KEYS", pattern))
|
||||
local result = self:command_ex({"KEYS", pattern})
|
||||
return unescape_array(result)
|
||||
end
|
||||
|
||||
@@ -178,7 +214,7 @@ end
|
||||
@return 返回 1 表示设置成功,0 表示键不存在或设置失败
|
||||
]]
|
||||
function redis_conn:expire(key, seconds)
|
||||
return self:command(build_command("EXPIRE", key, seconds))
|
||||
return self:command_ex({"EXPIRE", key, seconds})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -187,7 +223,7 @@ end
|
||||
@return 返回剩余秒数,-1 表示永不过期,-2 表示键不存在
|
||||
]]
|
||||
function redis_conn:ttl(key)
|
||||
return self:command(build_command("TTL", key))
|
||||
return self:command_ex({"TTL", key})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -196,7 +232,7 @@ end
|
||||
@return 返回增加后的值
|
||||
]]
|
||||
function redis_conn:incr(key)
|
||||
return self:command(build_command("INCR", key))
|
||||
return self:command_ex({"INCR", key})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -205,7 +241,7 @@ end
|
||||
@return 返回减少后的值
|
||||
]]
|
||||
function redis_conn:decr(key)
|
||||
return self:command(build_command("DECR", key))
|
||||
return self:command_ex({"DECR", key})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -215,7 +251,7 @@ end
|
||||
@return 返回增加后的值
|
||||
]]
|
||||
function redis_conn:incrby(key, increment)
|
||||
return self:command(build_command("INCRBY", key, increment))
|
||||
return self:command_ex({"INCRBY", key, increment})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -225,7 +261,7 @@ end
|
||||
@return 返回减少后的值
|
||||
]]
|
||||
function redis_conn:decrby(key, decrement)
|
||||
return self:command(build_command("DECRBY", key, decrement))
|
||||
return self:command_ex({"DECRBY", key, decrement})
|
||||
end
|
||||
|
||||
-- ==================== Hash 操作 ====================
|
||||
@@ -237,7 +273,7 @@ end
|
||||
@return 返回字段的值,不存在返回 nil
|
||||
]]
|
||||
function redis_conn:hget(key, field)
|
||||
local result = self:command(build_command("HGET", key, field))
|
||||
local result = self:command_ex({"HGET", key, field})
|
||||
return unescape_value(result)
|
||||
end
|
||||
|
||||
@@ -249,7 +285,7 @@ end
|
||||
@return 返回 1 表示新建字段,0 表示更新字段
|
||||
]]
|
||||
function redis_conn:hset(key, field, value)
|
||||
return self:command(build_command("HSET", key, field, value))
|
||||
return self:command_ex({"HSET", key, field, value})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -259,7 +295,8 @@ end
|
||||
@return 返回删除的字段数量
|
||||
]]
|
||||
function redis_conn:hdel(key, ...)
|
||||
return self:command(build_command("HDEL", key, ...))
|
||||
local args = {"HDEL", key, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -269,7 +306,7 @@ end
|
||||
@return 返回 1 表示存在,0 表示不存在
|
||||
]]
|
||||
function redis_conn:hexists(key, field)
|
||||
return self:command(build_command("HEXISTS", key, field))
|
||||
return self:command_ex({"HEXISTS", key, field})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -278,7 +315,7 @@ end
|
||||
@return 返回字段和值的数组
|
||||
]]
|
||||
function redis_conn:hgetall(key)
|
||||
local result = self:command(build_command("HGETALL", key))
|
||||
local result = self:command_ex({"HGETALL", key})
|
||||
return unescape_array(result)
|
||||
end
|
||||
|
||||
@@ -288,7 +325,7 @@ end
|
||||
@return 返回字段名数组
|
||||
]]
|
||||
function redis_conn:hkeys(key)
|
||||
local result = self:command(build_command("HKEYS", key))
|
||||
local result = self:command_ex({"HKEYS", key})
|
||||
return unescape_array(result)
|
||||
end
|
||||
|
||||
@@ -298,7 +335,7 @@ end
|
||||
@return 返回值数组
|
||||
]]
|
||||
function redis_conn:hvals(key)
|
||||
local result = self:command(build_command("HVALS", key))
|
||||
local result = self:command_ex({"HVALS", key})
|
||||
return unescape_array(result)
|
||||
end
|
||||
|
||||
@@ -308,7 +345,7 @@ end
|
||||
@return 返回字段数量
|
||||
]]
|
||||
function redis_conn:hlen(key)
|
||||
return self:command(build_command("HLEN", key))
|
||||
return self:command_ex({"HLEN", key})
|
||||
end
|
||||
|
||||
-- ==================== List 操作 ====================
|
||||
@@ -320,7 +357,8 @@ end
|
||||
@return 返回插入后列表的长度
|
||||
]]
|
||||
function redis_conn:lpush(key, ...)
|
||||
return self:command(build_command("LPUSH", key, ...))
|
||||
local args = {"LPUSH", key, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -330,7 +368,8 @@ end
|
||||
@return 返回插入后列表的长度
|
||||
]]
|
||||
function redis_conn:rpush(key, ...)
|
||||
return self:command(build_command("RPUSH", key, ...))
|
||||
local args = {"RPUSH", key, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -339,7 +378,7 @@ end
|
||||
@return 返回弹出的值,列表为空返回 nil
|
||||
]]
|
||||
function redis_conn:lpop(key)
|
||||
local result = self:command(build_command("LPOP", key))
|
||||
local result = self:command_ex({"LPOP", key})
|
||||
return unescape_value(result)
|
||||
end
|
||||
|
||||
@@ -349,7 +388,7 @@ end
|
||||
@return 返回弹出的值,列表为空返回 nil
|
||||
]]
|
||||
function redis_conn:rpop(key)
|
||||
local result = self:command(build_command("RPOP", key))
|
||||
local result = self:command_ex({"RPOP", key})
|
||||
return unescape_value(result)
|
||||
end
|
||||
|
||||
@@ -359,7 +398,7 @@ end
|
||||
@return 返回列表长度
|
||||
]]
|
||||
function redis_conn:llen(key)
|
||||
return self:command(build_command("LLEN", key))
|
||||
return self:command_ex({"LLEN", key})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -370,7 +409,7 @@ end
|
||||
@return 返回元素数组
|
||||
]]
|
||||
function redis_conn:lrange(key, start, stop)
|
||||
local result = self:command(build_command("LRANGE", key, start, stop))
|
||||
local result = self:command_ex({"LRANGE", key, start, stop})
|
||||
return unescape_array(result)
|
||||
end
|
||||
|
||||
@@ -383,7 +422,8 @@ end
|
||||
@return 返回添加的成员数量
|
||||
]]
|
||||
function redis_conn:sadd(key, ...)
|
||||
return self:command(build_command("SADD", key, ...))
|
||||
local args = {"SADD", key, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -393,7 +433,8 @@ end
|
||||
@return 返回移除的成员数量
|
||||
]]
|
||||
function redis_conn:srem(key, ...)
|
||||
return self:command(build_command("SREM", key, ...))
|
||||
local args = {"SREM", key, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -402,7 +443,7 @@ end
|
||||
@return 返回成员数组
|
||||
]]
|
||||
function redis_conn:smembers(key)
|
||||
local result = self:command(build_command("SMEMBERS", key))
|
||||
local result = self:command_ex({"SMEMBERS", key})
|
||||
return unescape_array(result)
|
||||
end
|
||||
|
||||
@@ -413,7 +454,7 @@ end
|
||||
@return 返回 1 表示存在,0 表示不存在
|
||||
]]
|
||||
function redis_conn:sismember(key, member)
|
||||
return self:command(build_command("SISMEMBER", key, member))
|
||||
return self:command_ex({"SISMEMBER", key, member})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -422,7 +463,7 @@ end
|
||||
@return 返回成员数量
|
||||
]]
|
||||
function redis_conn:scard(key)
|
||||
return self:command(build_command("SCARD", key))
|
||||
return self:command_ex({"SCARD", key})
|
||||
end
|
||||
|
||||
-- ==================== Sorted Set 操作 ====================
|
||||
@@ -436,8 +477,8 @@ end
|
||||
@return 返回添加的成员数量
|
||||
]]
|
||||
function redis_conn:zadd(key, score, member, ...)
|
||||
local args = {key, score, member, ...}
|
||||
return self:command(build_command("ZADD", table.unpack(args)))
|
||||
local args = {"ZADD", key, score, member, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -449,11 +490,11 @@ end
|
||||
@return 返回成员数组(如果 withscores 为 true,则包含分数)
|
||||
]]
|
||||
function redis_conn:zrange(key, start, stop, withscores)
|
||||
local cmd = build_command("ZRANGE", key, start, stop)
|
||||
local args = {"ZRANGE", key, start, stop}
|
||||
if withscores then
|
||||
cmd = cmd .. " WITHSCORES"
|
||||
table.insert(args, "WITHSCORES")
|
||||
end
|
||||
local result = self:command(cmd)
|
||||
local result = self:command_ex(args)
|
||||
if result == nil or type(result) ~= "table" then
|
||||
return result
|
||||
end
|
||||
@@ -483,7 +524,8 @@ end
|
||||
@return 返回移除的成员数量
|
||||
]]
|
||||
function redis_conn:zrem(key, ...)
|
||||
return self:command(build_command("ZREM", key, ...))
|
||||
local args = {"ZREM", key, ...}
|
||||
return self:command_ex(args)
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -492,7 +534,7 @@ end
|
||||
@return 返回成员数量
|
||||
]]
|
||||
function redis_conn:zcard(key)
|
||||
return self:command(build_command("ZCARD", key))
|
||||
return self:command_ex({"ZCARD", key})
|
||||
end
|
||||
|
||||
--[[
|
||||
@@ -502,7 +544,7 @@ end
|
||||
@return 返回分数,成员不存在返回 nil
|
||||
]]
|
||||
function redis_conn:zscore(key, member)
|
||||
local result = self:command(build_command("ZSCORE", key, member))
|
||||
local result = self:command_ex({"ZSCORE", key, member})
|
||||
-- 分数通常是数字,但为了统一处理,也进行去转义
|
||||
if result ~= nil then
|
||||
return unescape_value(result)
|
||||
|
||||
Reference in New Issue
Block a user