Skip to content

Execute Custom Scripts

Valkey scripting allows you to execute Lua code directly on the Valkey server. This guide will go over executing custom Lua scripts with GLIDE.

  • Running Valkey server (standalone or cluster)
  • GLIDE installed.

The following steps shows how to run a simple a custom Lua script using GLIDE.

  1. Define the lua script

    lua = """
    server.call('SET', KEYS[1], ARGV[1])
    return KEYS[1] .. ': ' .. server.call('GET', KEYS[1])
    """
  2. Create a Script object with the Lua code

    script = Script(lua)
  3. Execute script with keys and arguments

    keys = ["username"]
    args = ["John Doe"]
    result = await client.invoke_script(script, keys=keys, args=args)
    print(result) # b'username: John Doe'
Full Example
import asyncio
from glide import Script, GlideClient, GlideClientConfiguration, NodeAddress
async def main():
config = GlideClientConfiguration(addresses=[NodeAddress("localhost", 6379)])
client = await GlideClient.create(config)
lua = """
server.call('SET', KEYS[1], ARGV[1])
return KEYS[1] .. ': ' .. server.call('GET', KEYS[1])
"""
script = Script(lua)
keys = ["username"]
args = ["John Doe"]
result = await client.invoke_script(script, keys=keys, args=args)
print(result)
if __name__ == "__main__":
asyncio.run(main())

See our documentations for more on how GLIDE support scripting.