diff --git a/server/global/const.go b/server/global/const.go index 4ecad226..66767c93 100644 --- a/server/global/const.go +++ b/server/global/const.go @@ -997,6 +997,9 @@ const ( // LuaFNRedisHIncrBy represents the function name for "redis_hincrby" in Lua. LuaFNRedisHIncrBy = "redis_hincrby" + // LuaFnRedisHExists represents the Lua function name for checking if a field exists in a Redis hash. + LuaFnRedisHExists = "redis_hexists" + // LuaFnApplyBackendResult applies changes to the backend result from a former authentication process. LuaFnApplyBackendResult = "apply_backend_result" diff --git a/server/lualib/rediscli.go b/server/lualib/rediscli.go index 4042a7d9..36bda1a0 100644 --- a/server/lualib/rediscli.go +++ b/server/lualib/rediscli.go @@ -436,6 +436,42 @@ func RedisHIncrBy(L *lua.LState) int { return 1 } +// RedisHExists is a function which checks if the given field exists in the Redis hash stored at key. +// It accepts two flags, 'key' and 'field' to define the location of the data. +// This function interacts with the Redis instance through the ReadHandle. +// If an error occurs during the operation, the Lua state is pushed with 'nil' and the error message. +// If the operation is successful, the Lua state is pushed with either LTrue or LFalse, indicating the existence of the given field. +// +// Parameters: +// +// L *lua.LState: The lua state +// +// Returns: +// +// int: The status of the operation. If an error occurs, 2 is returned, otherwise 1. +func RedisHExists(L *lua.LState) int { + key := L.CheckString(1) + field := L.CheckString(2) + + exists, err := rediscli.ReadHandle.HExists(ctx, key, field).Result() + if err != nil { + L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) + + return 2 + } else { + stats.RedisReadCounter.Inc() + } + + if exists { + L.Push(lua.LTrue) + } else { + L.Push(lua.LFalse) + } + + return 1 +} + // SetUPRedisFunctions is a function that associates a set of Redis-related functions to a Lua table. // Each function is linked to a string that corresponds to its name in the global Lua functions namespace. // The provided Lua state `L` and the Lua table `table` are used to facilitate this setting up process. @@ -463,4 +499,5 @@ func SetUPRedisFunctions(table *lua.LTable, L *lua.LState) { table.RawSetString(global.LuaFnRedisHLen, L.NewFunction(RedisHLen)) table.RawSetString(global.LuaFnRedisHGetAll, L.NewFunction(RedisHGetAll)) table.RawSetString(global.LuaFNRedisHIncrBy, L.NewFunction(RedisHIncrBy)) + table.RawSetString(global.LuaFnRedisHExists, L.NewFunction(RedisHExists)) }