Code
redis-cli
# EVAL: run Lua inline. Keys start at KEYS[1], args at ARGV[1]
127.0.0.1:6379> EVAL "return redis.call('SET', KEYS[1], ARGV[1])" 1 mykey hello
OK
127.0.0.1:6379> EVAL "return redis.call('GET', KEYS[1])" 1 mykey
"hello"
# Atomic conditional set (only if not exists)
127.0.0.1:6379> EVAL \
"if redis.call('EXISTS', KEYS[1]) == 0 then \
return redis.call('SET', KEYS[1], ARGV[1]) \
else return 0 end" 1 lock:resource token-abc
# Cache the script — call by SHA1 (avoids resending the body)
127.0.0.1:6379> SCRIPT LOAD "return redis.call('GET', KEYS[1])"
"e0e1f9fabfc9d4800c877a703b823ac0578ff833"
127.0.0.1:6379> EVALSHA e0e1f9fabfc9d4800c877a703b823ac0578ff833 1 mykey
"hello"
127.0.0.1:6379> SCRIPT EXISTS e0e1f9fabfc9d4800c877a703b823ac0578ff833
# Functions (Redis 7+) — named, library-style, replaces EVAL for new apps
127.0.0.1:6379> FUNCTION LOAD '#!lua name=mylib \
redis.register_function("myget", function(keys, args) \
return redis.call("GET", keys[1]) end)'
127.0.0.1:6379> FCALL myget 1 mykey
# List / dump functions
127.0.0.1:6379> FUNCTION LIST
127.0.0.1:6379> FUNCTION DUMP