Skip to content

Commit

Permalink
Add RedisHExists function to check field existence in Redis hash
Browse files Browse the repository at this point in the history
The new function, RedisHExists, has been added to the lualib. It checks whether a particular field exists in a Redis hash stored at a specified key. It interacts with the Redis instance via the ReadHandle, providing an error message or the existence of the field. Also, the function name 'redis_hexists' has been added to global const for ease of use.

Signed-off-by: Christian Roessner <c@roessner.co>
  • Loading branch information
Christian Roessner committed Jul 3, 2024
1 parent 09c0bb8 commit ef4964f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/global/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
37 changes: 37 additions & 0 deletions server/lualib/rediscli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}

0 comments on commit ef4964f

Please sign in to comment.