Skip to content
Jennifer Programming Language

redis API reference

A Redis client speaking RESP2 (the REdis Serialization Protocol) over the net system library. Commands go out as RESP arrays of bulk strings; replies (+OK, -ERR, :int, $bulk, *array) parse back into a Reply. Typed per-command helpers (get / set / incr / keys / ...) keep the common path fully typed; command is the generic escape hatch for anything else. A -ERR reply throws a catchable redis.RedisError (kind "redis") whose code is the reply's first word (ERR, WRONGTYPE, NOAUTH, MOVED, ...). The reply parser frames over bytes and counts bulk-string lengths in bytes, so a value whose byte length differs from its rune length (any non-ASCII UTF-8 text) is read byte-exact. kvStore wraps a session as a kvstore.Store, the backend session and ratelimit take. Needs the default jennifer binary (uses net).

Import with import "redis.j" as redis;. See the redis guide for prose and examples.

Contents

NameWhat it is
commandSend one command (its arguments) and return the reply.
connectOpen a session, authenticating and selecting a database when set.
decrAtomically decrement key and return the new value.
delDelete key and return the number of keys removed (0 or 1).
discardDiscard the queued transaction (DISCARD).
execExecute the queued transaction (EXEC) and return one reply per queued command, in order.
existsReport whether key is present.
getReturn the string value of key, or "" when the key is missing.
getBytesReturn the raw bytes value of key, or empty bytes when the key is missing (use exists to tell an em...
hdelDelete field from hash key (HDEL); returns the number removed (0 or 1).
hgetReturn hash key's field (HGET), or "" when the field or key is missing.
hgetAllReturn every field and value of hash key (HGETALL) as a map of string to string (empty when the key is ...
hsetSet hash key's field to value (HSET); returns the number of fields newly created (0 when it already e...
incrAtomically increment key and return the new value.
keysReturn the keys matching a glob pattern (e.g.
kvStoreA kvstore.Store over this session (a distributed backend for kvstore, session and ratelimit):...
llenReturn the length of list key (LLEN); 0 when the key is missing.
lpopRemove and return the first element of list key (LPOP), or "" when empty.
lpushPrepend value to list key (LPUSH); returns the list's new length.
lrangeReturn the elements of list key from start to stop inclusive (LRANGE; negative indices count from the...
multiBegin a transaction (MULTI).
pingReturn the server's PONG (a liveness check).
pipelineSend several commands in one write and read exactly one reply per command (a single network round trip).
psubscribeSubscribe to one or more glob patterns (e.g.
publishPublish message to channel and return the number of subscribers that received it.
punsubscribeUnsubscribe from the given patterns, or from all patterns when the list is empty, and wait until the server...
quitEnd the session and close the connection.
receiveMessageBlock until the next message / pmessage push arrives, skipping the (un)subscribe confirmation frames.
rpushAppend value to list key (RPUSH); returns the list's new length.
saddAdd member to set key (SADD); returns the number newly added (0 or 1).
scanWalk the keyspace one page at a time (SCAN), the production-safe iterator: start with cursor 0 and repeat...
scardReturn the number of members in set key (SCARD); 0 when missing.
setStore value at key.
setBytesStore a raw bytes value at key, byte-for-byte.
sismemberReport whether member is in set key (SISMEMBER).
smembersReturn every member of set key (SMEMBERS) as a list (empty when missing; order is unspecified).
sremRemove member from set key (SREM); returns the number removed (0 or 1).
subscribeSubscribe to one or more channels and wait until the server has confirmed it, so a message published once t...
unsubscribeUnsubscribe from the given channels, or from all channels when the list is empty, and wait until the server...
Messagestruct: A pushed pub/sub message (from receiveMessage).
Optionsstruct: Connection settings.
RedisErrorstruct: A failed Redis command.
Replystruct: A parsed RESP reply.
ScanResultstruct: One step of a scan cursor walk.
Sessionstruct: An open Redis connection.

Functions

command

jennifer
redis.command(session as Session, args as list of string)

Send one command (its arguments) and return the reply.

session
the open session
args
the command name and its arguments
Returns Reply
the parsed reply
Throws RedisError
kind "redis" on a -ERR reply

connect

jennifer
redis.connect(opts as Options)

Open a session, authenticating and selecting a database when set.

opts
the connection settings
Returns Session
the open session

decr

jennifer
redis.decr(session as Session, key as string)

Atomically decrement key and return the new value.

session
the open session
key
the counter key
Returns int
the new value

del

jennifer
redis.del(session as Session, key as string)

Delete key and return the number of keys removed (0 or 1).

session
the open session
key
the key to delete
Returns int
the number of keys removed (0 or 1)

discard

jennifer
redis.discard(session as Session)

Discard the queued transaction (DISCARD).

session
the open session

exec

jennifer
redis.exec(session as Session)

Execute the queued transaction (EXEC) and return one reply per queued command, in order. An aborted transaction (a nil EXEC reply) yields an empty list.

session
the open session
Returns list of Reply
the queued commands' replies

exists

jennifer
redis.exists(session as Session, key as string)

Report whether key is present.

session
the open session
key
the key to test
Returns bool
whether the key exists

get

jennifer
redis.get(session as Session, key as string)

Return the string value of key, or "" when the key is missing.

session
the open session
key
the key to read
Returns string
the value, or "" when missing

getBytes

jennifer
redis.getBytes(session as Session, key as string)

Return the raw bytes value of key, or empty bytes when the key is missing (use exists to tell an empty value from a missing key). The byte-exact counterpart to get: it never UTF-8-decodes, so a binary value stored with setBytes round-trips exactly.

session
the open session
key
the key to read
Returns bytes
the value, or empty bytes when missing
Throws RedisError
kind "redis" on a -ERR reply

hdel

jennifer
redis.hdel(session as Session, key as string, field as string)

Delete field from hash key (HDEL); returns the number removed (0 or 1).

session
the open session
key
the hash key
field
the field to delete
Returns int
the number of fields removed

hget

jennifer
redis.hget(session as Session, key as string, field as string)

Return hash key's field (HGET), or "" when the field or key is missing.

session
the open session
key
the hash key
field
the field name
Returns string
the value, or "" when missing

hgetAll

jennifer
redis.hgetAll(session as Session, key as string)

Return every field and value of hash key (HGETALL) as a map of string to string (empty when the key is missing).

session
the open session
key
the hash key
Returns map of string to string
the field -> value pairs

hset

jennifer
redis.hset(
    session as Session,
    key as string,
    field as string,
    value as string)

Set hash key's field to value (HSET); returns the number of fields newly created (0 when it already existed and was updated).

session
the open session
key
the hash key
field
the field name
value
the field value
Returns int
the number of new fields (0 or 1)

incr

jennifer
redis.incr(session as Session, key as string)

Atomically increment key and return the new value.

session
the open session
key
the counter key
Returns int
the new value

keys

jennifer
redis.keys(session as Session, pattern as string)

Return the keys matching a glob pattern (e.g. "", "user:").

session
the open session
pattern
the glob pattern
Returns list of string
the matching keys

kvStore

jennifer
redis.kvStore(session as Session)

A kvstore.Store over this session (a distributed backend for kvstore, session and ratelimit): kvstore.set / get / delete / touch / incrWindow then run on the server. The session must not be subscribed.

session
the open session
Returns kvstore.Store
a redis-backed store

llen

jennifer
redis.llen(session as Session, key as string)

Return the length of list key (LLEN); 0 when the key is missing.

session
the open session
key
the list key
Returns int
the list length

lpop

jennifer
redis.lpop(session as Session, key as string)

Remove and return the first element of list key (LPOP), or "" when empty.

session
the open session
key
the list key
Returns string
the popped element, or "" when the list is empty

lpush

jennifer
redis.lpush(session as Session, key as string, value as string)

Prepend value to list key (LPUSH); returns the list's new length.

session
the open session
key
the list key
value
the value to prepend
Returns int
the new list length

lrange

jennifer
redis.lrange(session as Session, key as string, start as int, stop as int)

Return the elements of list key from start to stop inclusive (LRANGE; negative indices count from the end, so 0, -1 is the whole list).

session
the open session
key
the list key
start
the start index
stop
the stop index (inclusive)
Returns list of string
the elements in range

multi

jennifer
redis.multi(session as Session)

Begin a transaction (MULTI). Commands issued after this are queued (each replies "+QUEUED") until exec runs them atomically or discard drops them.

session
the open session

ping

jennifer
redis.ping(session as Session)

Return the server's PONG (a liveness check).

session
the open session
Returns string
the server's reply ("PONG")

pipeline

jennifer
redis.pipeline(session as Session, commands as list of list of string)

Send several commands in one write and read exactly one reply per command (a single network round trip). Unlike command, a -ERR reply does not throw - each command's outcome is returned in order, so inspect each Reply.

session
the open session
commands
each command as an argument list
Returns list of Reply
one reply per command, in order

psubscribe

jennifer
redis.psubscribe(session as Session, patterns as list of string)

Subscribe to one or more glob patterns (e.g. "news.*") and wait until the server has confirmed it, as subscribe does.

session
the open session
patterns
the patterns to subscribe to
Throws RedisError
kind "redis" on a server error reply (an empty list is one)

publish

jennifer
redis.publish(session as Session, channel as string, message as string)

Publish message to channel and return the number of subscribers that received it. Runs on an ordinary (non-subscribed) connection.

session
the open session
channel
the channel to publish to
message
the message body
Returns int
the number of subscribers that received the message

punsubscribe

jennifer
redis.punsubscribe(session as Session, patterns as list of string)

Unsubscribe from the given patterns, or from all patterns when the list is empty, and wait until the server has confirmed it, as unsubscribe does.

session
the open session
patterns
the patterns to leave ([] means all)
Throws RedisError
kind "redis" on a server error reply

quit

jennifer
redis.quit(session as Session)

End the session and close the connection.

session
the open session

receiveMessage

jennifer
redis.receiveMessage(session as Session)

Block until the next message / pmessage push arrives, skipping the (un)subscribe confirmation frames. Messages come back in the order the server sent them, including those read while a subscribe waited for its confirmation and those that arrived in the same read as an earlier one. Wrap the call in a spawn to receive concurrently with the rest of the program (the session's copies share their read state, so another task may subscribe / unsubscribe meanwhile); the per-read idle timeout (Session.timeoutMs, milliseconds) bounds the wait and raises a catchable error on a stall.

session
the open, subscribed session
Returns Message
the next channel / pattern push
Throws RedisError
kind "redis" on a server error reply; kind "closed" when the server closes the connection

rpush

jennifer
redis.rpush(session as Session, key as string, value as string)

Append value to list key (RPUSH); returns the list's new length.

session
the open session
key
the list key
value
the value to append
Returns int
the new list length

sadd

jennifer
redis.sadd(session as Session, key as string, member as string)

Add member to set key (SADD); returns the number newly added (0 or 1).

session
the open session
key
the set key
member
the member to add
Returns int
the number of members added

scan

jennifer
redis.scan(
    session as Session,
    cursor as int,
    pattern as string,
    count as int)

Walk the keyspace one page at a time (SCAN), the production-safe iterator: start with cursor 0 and repeat until the returned cursor is 0 again. Pass pattern "" to skip the MATCH glob and count 0 to skip the COUNT hint. Prefer this over keys, which blocks the server on a large keyspace.

session
the open session
cursor
the cursor (0 to begin)
pattern
a glob to filter keys ("" for no filter)
count
a per-page size hint (0 for the server default)
Returns ScanResult
the next cursor and this page's keys

scard

jennifer
redis.scard(session as Session, key as string)

Return the number of members in set key (SCARD); 0 when missing.

session
the open session
key
the set key
Returns int
the member count

set

jennifer
redis.set(session as Session, key as string, value as string)

Store value at key.

session
the open session
key
the key to write
value
the value to store

setBytes

jennifer
redis.setBytes(session as Session, key as string, value as bytes)

Store a raw bytes value at key, byte-for-byte. Unlike set (whose string value is UTF-8-encoded onto the wire), this stores arbitrary binary - a serialized blob, a compressed payload, an image - which getBytes reads back exactly. Read it with getBytes, not get (the string reader throws on a non-UTF-8 value).

session
the open session
key
the key to write
value
the raw value to store
Throws RedisError
kind "redis" on a -ERR reply

sismember

jennifer
redis.sismember(session as Session, key as string, member as string)

Report whether member is in set key (SISMEMBER).

session
the open session
key
the set key
member
the member to test
Returns bool
whether the member is present

smembers

jennifer
redis.smembers(session as Session, key as string)

Return every member of set key (SMEMBERS) as a list (empty when missing; order is unspecified).

session
the open session
key
the set key
Returns list of string
the members

srem

jennifer
redis.srem(session as Session, key as string, member as string)

Remove member from set key (SREM); returns the number removed (0 or 1).

session
the open session
key
the set key
member
the member to remove
Returns int
the number of members removed

subscribe

jennifer
redis.subscribe(session as Session, channels as list of string)

Subscribe to one or more channels and wait until the server has confirmed it, so a message published once this returns is delivered. After subscribing, the connection is in subscribed mode and may only run (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT until fully unsubscribed; read pushes with receiveMessage. A message that arrives while this waits (on a connection already subscribed elsewhere) is kept for receiveMessage.

session
the open session
channels
the channels to subscribe to
Throws RedisError
kind "redis" on a server error reply (an empty list is one)

unsubscribe

jennifer
redis.unsubscribe(session as Session, channels as list of string)

Unsubscribe from the given channels, or from all channels when the list is empty, and wait until the server has confirmed it: no message from those channels arrives after this returns (one that arrived before stays queued for receiveMessage).

session
the open session
channels
the channels to leave ([] means all)
Throws RedisError
kind "redis" on a server error reply

Structs

Message

A pushed pub/sub message (from receiveMessage).

jennifer
def struct Message {
    kind as string,
    channel as string,
    pattern as string,
    payload as string
};
kind
"message" (a plain channel push) or "pmessage" (a pattern push)
channel
the channel the message was published to
pattern
the subscribed glob pattern ("" for a plain message)
payload
the message body

Options

Connection settings.

jennifer
def struct Options {
    host as string,
    port as int,
    security as transport.Security,
    user as string,
    password as string,
    db as int
};
host
the server host
port
the server port
security
transport.Security.None (plaintext) or .Tls (rediss); .Starttls is rejected (Redis has no in-band upgrade)
user
the AUTH username ("" for password-only or no auth)
password
the AUTH password; "" skips AUTH
db
the database to SELECT (0 is the default)

RedisError

A failed Redis command. Every error of kind "redis" is one, so its fields are safe to read once the kind matches.

jennifer
def struct RedisError {
    kind as string,
    message as string,
    code as string
};
kind
always "redis"
message
the server's error reply (WRONGTYPE Operation against a key ...), or what went wrong
code
the error reply's first word, its error code (ERR, WRONGTYPE, NOAUTH, WRONGPASS, MOVED, LOADING, READONLY, ...); "" for a failure that is no server error reply

Reply

A parsed RESP reply.

jennifer
def struct Reply {
    kind as string,
    str as string,
    num as int,
    items as list of Reply
};
kind
"string" (simple or bulk), "error", "int", "nil", or "array"
str
the string / error text
num
the integer value
items
an array reply's elements

ScanResult

One step of a scan cursor walk.

jennifer
def struct ScanResult {
    cursor as int,
    keys as list of string
};
cursor
the cursor to pass to the next scan (0 ends the walk)
keys
the keys returned by this step

Session

An open Redis connection. Build it with connect.

jennifer
def struct Session {
    conn as net.Conn,
    timeoutMs as int,
    pubsub as channel of PubSub
};
conn
the underlying socket
timeoutMs
per-read idle timeout in milliseconds (0 disables it)
pubsub
the shared pub/sub read state (internal)