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
| Name | What it is |
|---|---|
command | Send one command (its arguments) and return the reply. |
connect | Open a session, authenticating and selecting a database when set. |
decr | Atomically decrement key and return the new value. |
del | Delete key and return the number of keys removed (0 or 1). |
discard | Discard the queued transaction (DISCARD). |
exec | Execute the queued transaction (EXEC) and return one reply per queued command, in order. |
exists | Report whether key is present. |
get | Return the string value of key, or "" when the key is missing. |
getBytes | Return the raw bytes value of key, or empty bytes when the key is missing (use exists to tell an em... |
hdel | Delete field from hash key (HDEL); returns the number removed (0 or 1). |
hget | Return hash key's field (HGET), or "" when the field or key is missing. |
hgetAll | Return every field and value of hash key (HGETALL) as a map of string to string (empty when the key is ... |
hset | Set hash key's field to value (HSET); returns the number of fields newly created (0 when it already e... |
incr | Atomically increment key and return the new value. |
keys | Return the keys matching a glob pattern (e.g. |
kvStore | A kvstore.Store over this session (a distributed backend for kvstore, session and ratelimit):... |
llen | Return the length of list key (LLEN); 0 when the key is missing. |
lpop | Remove and return the first element of list key (LPOP), or "" when empty. |
lpush | Prepend value to list key (LPUSH); returns the list's new length. |
lrange | Return the elements of list key from start to stop inclusive (LRANGE; negative indices count from the... |
multi | Begin a transaction (MULTI). |
ping | Return the server's PONG (a liveness check). |
pipeline | Send several commands in one write and read exactly one reply per command (a single network round trip). |
psubscribe | Subscribe to one or more glob patterns (e.g. |
publish | Publish message to channel and return the number of subscribers that received it. |
punsubscribe | Unsubscribe from the given patterns, or from all patterns when the list is empty, and wait until the server... |
quit | End the session and close the connection. |
receiveMessage | Block until the next message / pmessage push arrives, skipping the (un)subscribe confirmation frames. |
rpush | Append value to list key (RPUSH); returns the list's new length. |
sadd | Add member to set key (SADD); returns the number newly added (0 or 1). |
scan | Walk the keyspace one page at a time (SCAN), the production-safe iterator: start with cursor 0 and repeat... |
scard | Return the number of members in set key (SCARD); 0 when missing. |
set | Store value at key. |
setBytes | Store a raw bytes value at key, byte-for-byte. |
sismember | Report whether member is in set key (SISMEMBER). |
smembers | Return every member of set key (SMEMBERS) as a list (empty when missing; order is unspecified). |
srem | Remove member from set key (SREM); returns the number removed (0 or 1). |
subscribe | Subscribe to one or more channels and wait until the server has confirmed it, so a message published once t... |
unsubscribe | Unsubscribe from the given channels, or from all channels when the list is empty, and wait until the server... |
Message | struct: A pushed pub/sub message (from receiveMessage). |
Options | struct: Connection settings. |
RedisError | struct: A failed Redis command. |
Reply | struct: A parsed RESP reply. |
ScanResult | struct: One step of a scan cursor walk. |
Session | struct: An open Redis connection. |
Functions
command
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
-ERRreply
connect
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
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
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
redis.discard(session as Session)Discard the queued transaction (DISCARD).
session- the open session
exec
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
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
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
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
-ERRreply
hdel
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
redis.quit(session as Session)End the session and close the connection.
session- the open session
receiveMessage
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
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
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
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
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
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
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
-ERRreply
sismember
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
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
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
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
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).
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.
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
securitytransport.Security.None(plaintext) or.Tls(rediss);.Starttlsis 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.
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.
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.
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.
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)