Skip to content

What it compiles to

Nothing is hidden. Every script exposes the Lua it produced:

>>> print(rate_limit.lua)
-- rate_limit
-- Generated by redis-lua-py from src/limits.py:6. Do not edit.
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local ttl = tonumber(ARGV[2])
local current = redis.call('INCR', key)
if current == 1 then
  redis.call('EXPIRE', key, ttl)
end
if current > limit then
  return -1
end
return limit - current

Read it in review, paste it into redis-cli, check it into a golden test. The point of this library is to generate Lua you would have been willing to write.

The header and the SHA

The header is part of the body, and the body is what EVALSHA hashes, so the path in it is relative to your project root rather than absolute — the same script has the same SHA on a laptop, in CI and in a container, and the server's script cache is cold once per script rather than once per environment.

Pass @script(header=False) to drop the comment entirely:

@script(header=False)
def rate_limit(key: Key, limit: int, ttl: int) -> int: ...

What else a script exposes

Attribute For the script above
.lua the full Lua source, exactly as sent to Redis
.keys ('key',) — the parameters annotated Key, in order
.args ('limit', 'ttl')
.name 'rate_limit'
.source where it was defined, repo-relative

A bound script exposes all of the same.