Skip to content

Commit cccb594

Browse files
committed
Simplify random_key wrapper
1 parent fddb536 commit cccb594

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

src/lua/ffi_api.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ function VKM.db_size(ctx)
271271
return tonumber(C.ValkeyModule_DbSize(ctx))
272272
end
273273

274-
function VKM.random_key(ctx)
274+
function VKM.random_key()
275+
local ctx = VKM.ctx()
276+
if ctx == nil then return VKM.ERR end
275277
local rms = C.ValkeyModule_RandomKey(ctx)
276278
if rms == nil then
277279
return nil

tests/scripting-ffi.tcl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ start_server {tags {"scripting-ffi"} overrides {luajit.enable-ffi-api yes}} {
4040
r set "key1" "value1"
4141
r set "key2" "value2"
4242
r eval "
43-
local ctx = VKM.ctx()
44-
local key = VKM.random_key(ctx)
43+
local key = VKM.random_key()
4544
return type(key)
4645
" 0
4746
} {string}
4847
r del "key1" "key2"
4948

5049
test {FFI - random_key returns nil for empty DB} {
5150
r eval "
52-
local ctx = VKM.ctx()
53-
local key = VKM.random_key(ctx)
51+
local key = VKM.random_key()
5452
if key == nil then
5553
return 'nil'
5654
else
@@ -62,8 +60,7 @@ start_server {tags {"scripting-ffi"} overrides {luajit.enable-ffi-api yes}} {
6260
test {FFI - random_key returns valid key from DB} {
6361
r set "testkey" "testvalue"
6462
r eval "
65-
local ctx = VKM.ctx()
66-
local key = VKM.random_key(ctx)
63+
local key = VKM.random_key()
6764
return key
6865
" 0
6966
} {testkey}

0 commit comments

Comments
 (0)