Skip to main content

Actions

The ballerinax/redis package exposes the following clients:

ClientPurpose
ClientProvides operations to interact with a Redis server or cluster for data storage and retrieval.

Client

Provides operations to interact with a Redis server or cluster for data storage and retrieval.

Configuration

The client is configured with a ConnectionConfig record.

FieldTypeDefaultDescription
connectionConnectionUri|ConnectionParams"redis://localhost:6379"Redis connection URI string or a ConnectionParams record with host, port, username, password, and options.
connectionPoolingbooleanfalseWhether to enable connection pooling.
isClusterConnectionbooleanfalseWhether this is a Redis cluster connection.
secureSocketSecureSocket?()SSL/TLS configuration for encrypted connections.

Configuration types

ConnectionUri

A Redis connection URI string. Aliases string.

Format: redis://[username:password@]host[:port][/database] for unencrypted, or rediss://... for TLS.

ConnectionParams

Field-based connection configuration.

FieldTypeDefaultDescription
hoststring"localhost"Host address of the Redis database.
portint6379Port of the Redis database.
usernamestring?()Username for the Redis database (Redis 6.0+ ACL).
passwordstring?()Password for the Redis database.
optionsOptions{}Additional connection options.

Options

Connection options.

FieldTypeDefaultDescription
clientNamestring?()Name of the client.
databaseint0Database index the client should interact with. Not applicable for cluster connections.
connectionTimeoutint60Connection timeout in seconds.
keepAliveKeepAliveConfig?()TCP keep-alive configuration for detecting stale connections. Set to () to disable.

KeepAliveConfig

TCP keep-alive parameters.

FieldTypeDefaultDescription
idleint7200Time in seconds the connection must be idle before the first keep-alive probe is sent.
intervalint75Time in seconds between individual keep-alive probes.
countint9Maximum number of keep-alive probes before the connection is considered dead.

SecureSocket

SSL/TLS configuration.

FieldTypeDefaultDescription
certcrypto:TrustStore|string?()A crypto:TrustStore record ({path, password}) or a path to a single certificate file the client trusts.
keycrypto:KeyStore|CertKey?()A crypto:KeyStore record ({path, password}) or a CertKey value (used for mutual TLS).
protocolsstring[]?()List of protocols used for the connection, such as TLSv1.2, TLSv1.1, TLSv1.
ciphersstring[]?()List of ciphers to be used for SSL connections.
verifyModeSslVerifyModeFULLServer-certificate verification mode.
startTlsbooleanfalseWhether StartTLS is enabled.

CertKey

Client certificate and private key (used for mutual TLS).

FieldTypeDefaultDescription
certFilestringRequiredPath to the certificate file.
keyFilestringRequiredPath to the private key file in PKCS8 format.
keyPasswordstring?()Password of the private key, if encrypted.

SslVerifyMode

Enum of TLS verification modes:

  • NONE: No verification.
  • CA: Verify the server's certificate against the provided CA certificates.
  • FULL: Verify the server's certificate against the provided CA certificates and also verify the server's hostname.

Initializing the client

Using a connection URI:

import ballerinax/redis;

redis:Client redis = check new ({
connection: "redis://localhost:6379"
});

Or using connection parameters with authentication:

import ballerinax/redis;

redis:Client redis = check new ({
connection: {
host: "localhost",
port: 6379,
password: "my_password"
}
});

Operations

String operations

set

Set the string value of a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to set.
valuestringYesThe value to set.

Returns: string|redis:Error

Sample code:

string result = check redis->set("name", "John");

Sample response:

"OK"
get

Get the value of a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to retrieve.

Returns: string|redis:Error?

Sample code:

string? value = check redis->get("name");

Sample response:

"John"
append

Append a value to a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to append to.
valuestringYesThe value to append.

Returns: int|redis:Error

Sample code:

int length = check redis->append("greeting", " World");

Sample response:

11
incr

Increment the integer value of a key by one.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to increment.

Returns: int|redis:Error

Sample code:

int newValue = check redis->incr("counter");

Sample response:

1
incrBy

Increment the integer value of a key by the given amount.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to increment.
valueintYesThe increment amount.

Returns: int|redis:Error

Sample code:

int newValue = check redis->incrBy("counter", 5);

Sample response:

6
incrByFloat

Increment the float value of a key by the given amount.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to increment.
valuefloatYesThe float increment amount.

Returns: float|redis:Error

Sample code:

float newValue = check redis->incrByFloat("price", 2.5);

Sample response:

12.5
decr

Decrement the integer value of a key by one.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to decrement.

Returns: int|redis:Error

Sample code:

int newValue = check redis->decr("counter");

Sample response:

4
decrBy

Decrement the integer value of a key by the given number.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to decrement.
valueintYesThe decrement amount.

Returns: int|redis:Error

Sample code:

int newValue = check redis->decrBy("counter", 3);

Sample response:

1
mSet

Set multiple keys to multiple values.

Parameters:

NameTypeRequiredDescription
keyValueMapmap<any>YesA map of key-value pairs to set.

Returns: string|redis:Error

Sample code:

string result = check redis->mSet({
"key1": "value1",
"key2": "value2",
"key3": "value3"
});

Sample response:

"OK"
mGet

Get the values of all the given keys.

Parameters:

NameTypeRequiredDescription
keysstring[]YesArray of keys to retrieve.

Returns: string[]|redis:Error

Sample code:

string[] values = check redis->mGet(["key1", "key2", "key3"]);

Sample response:

["value1", "value2", "value3"]
mSetNx

Set multiple keys to multiple values, only if none of the keys exist.

Parameters:

NameTypeRequiredDescription
keyValueMapmap<any>YesA map of key-value pairs to set.

Returns: boolean|redis:Error

Sample code:

boolean result = check redis->mSetNx({"nx1": "val1", "nx2": "val2"});

Sample response:

true
setEx

Set the value and expiration (in seconds) of a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to set.
valuestringYesThe value to set.
expirationTimeintYesExpiration time in seconds.

Returns: string|redis:Error

Sample code:

string result = check redis->setEx("session:abc123", "user_data", 3600);

Sample response:

"OK"
pSetEx

Set value and expiration in milliseconds of a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to set.
valuestringYesThe value to set.
expirationTimeintYesExpiration time in milliseconds.

Returns: string|redis:Error

Sample code:

string result = check redis->pSetEx("temp:data", "ephemeral", 5000);

Sample response:

"OK"
setNx

Set the value of a key, only if the key does not exist.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to set.
valuestringYesThe value to set.

Returns: boolean|redis:Error

Sample code:

boolean wasSet = check redis->setNx("lock:resource1", "locked");

Sample response:

true
getSet

Set the string value of a key and return its old value.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to set.
valuestringYesThe new value.

Returns: string|redis:Error?

Sample code:

string? oldValue = check redis->getSet("status", "active");

Sample response:

"inactive"
getRange

Get a substring of the string stored at a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.
startPosintYesStart offset (inclusive).
endintYesEnd offset (inclusive).

Returns: string|redis:Error

Sample code:

string substring = check redis->getRange("greeting", 0, 4);

Sample response:

"Hello"
setRange

Overwrite part of a string at key starting at the specified offset.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.
offsetintYesThe byte offset to start overwriting at.
valuestringYesThe value to write.

Returns: int|redis:Error

Sample code:

int newLen = check redis->setRange("greeting", 6, "Redis");

Sample response:

11
strLen

Get the length of the value stored in a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.

Returns: int|redis:Error

Sample code:

int length = check redis->strLen("name");

Sample response:

4
bitCount

Count the number of set bits (population counting) in a string.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.

Returns: int|redis:Error

Sample code:

int bits = check redis->bitCount("mybitfield");

Sample response:

6
setBit

Sets or clears the bit at offset in the string value stored at key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.
valueintYesThe bit value (0 or 1).
offsetintYesThe bit offset.

Returns: int|redis:Error

Sample code:

int originalBit = check redis->setBit("mybitfield", 1, 7);

Sample response:

0
getBit

Returns the bit value at offset in the string value stored at key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.
offsetintYesThe bit offset.

Returns: int|redis:Error

Sample code:

int bit = check redis->getBit("mybitfield", 7);

Sample response:

1
bitOpAnd

Perform a bitwise AND operation between multiple strings and store the result.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe source keys.

Returns: int|redis:Error

Sample code:

int resultLen = check redis->bitOpAnd("result", ["key1", "key2"]);

Sample response:

1
bitOpOr

Perform a bitwise OR operation between multiple strings and store the result.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe source keys.

Returns: int|redis:Error

Sample code:

int resultLen = check redis->bitOpOr("result", ["key1", "key2"]);

Sample response:

1
bitOpNot

Perform a bitwise NOT operation on a string and store the result.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keystringYesThe source key.

Returns: int|redis:Error

Sample code:

int resultLen = check redis->bitOpNot("result", "source");

Sample response:

1
bitOpXor

Perform a bitwise XOR operation between multiple strings and store the result.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe source keys.

Returns: int|redis:Error

Sample code:

int resultLen = check redis->bitOpXor("result", ["key1", "key2"]);

Sample response:

1

List operations

lPush

Prepend one or multiple values to a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
valuesstring[]YesThe values to prepend.

Returns: int|redis:Error

Sample code:

int listLen = check redis->lPush("tasks", ["task1", "task2", "task3"]);

Sample response:

3
rPush

Append one or multiple values to a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
valuesstring[]YesThe values to append.

Returns: int|redis:Error

Sample code:

int listLen = check redis->rPush("queue", ["item1", "item2"]);

Sample response:

2
lPop

Remove and get the first element in a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.

Returns: string|redis:Error?

Sample code:

string? first = check redis->lPop("tasks");

Sample response:

"task3"
rPop

Remove and get the last element in a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.

Returns: string|redis:Error?

Sample code:

string? last = check redis->rPop("tasks");

Sample response:

"task1"
lPushX

Prepend values to a list, only if the list exists.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
valuesstring[]YesThe values to prepend.

Returns: int|redis:Error

Sample code:

int listLen = check redis->lPushX("tasks", ["urgent_task"]);

Sample response:

4
rPushX

Append values to a list, only if the list exists.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
valuesstring[]YesThe values to append.

Returns: int|redis:Error

Sample code:

int listLen = check redis->rPushX("queue", ["late_item"]);

Sample response:

3
bLPop

Remove and get the first element in a list, or block until one is available.

Parameters:

NameTypeRequiredDescription
timeOutintYesTimeout in seconds (0 to block indefinitely).
keysstring[]YesThe list keys to pop from.

Returns: map<any>|redis:Error

Sample code:

map<any> result = check redis->bLPop(30, ["queue"]);

Sample response:

{"queue": "item1"}
bRPop

Remove and get the last element in a list, or block until one is available.

Parameters:

NameTypeRequiredDescription
timeoutintYesTimeout in seconds (0 to block indefinitely).
keysstring[]YesThe list keys to pop from.

Returns: map<any>|redis:Error

Sample code:

map<any> result = check redis->bRPop(30, ["queue"]);

Sample response:

{"queue": "item2"}
lIndex

Get an element from a list by its index.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
indexintYesZero-based index (negative indices count from the end).

Returns: string|redis:Error?

Sample code:

string? element = check redis->lIndex("tasks", 0);

Sample response:

"task1"
lInsert

Insert an element before or after another element in a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
beforebooleanYestrue to insert before the pivot, false to insert after.
pivotstringYesThe reference element.
valuestringYesThe value to insert.

Returns: int|redis:Error

Sample code:

int listLen = check redis->lInsert("tasks", true, "task2", "task1.5");

Sample response:

4
lLen

Get the length of a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.

Returns: int|redis:Error

Sample code:

int length = check redis->lLen("tasks");

Sample response:

3
lRange

Get a range of elements from a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
startPosintYesStart index (inclusive).
stopPosintYesStop index (inclusive).

Returns: string[]|redis:Error

Sample code:

string[] elements = check redis->lRange("tasks", 0, -1);

Sample response:

["task1", "task2", "task3"]
lRem

Remove elements from a list.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
countintYesNumber of occurrences to remove (0 = all, positive = from head, negative = from tail).
valuestringYesThe value to remove.

Returns: int|redis:Error

Sample code:

int removed = check redis->lRem("tasks", 1, "task2");

Sample response:

1
lSet

Set the value of an element in a list by its index.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
indexintYesThe index of the element.
valuestringYesThe new value.

Returns: string|redis:Error

Sample code:

string result = check redis->lSet("tasks", 0, "updated_task");

Sample response:

"OK"
lTrim

Trim a list to the specified range.

Parameters:

NameTypeRequiredDescription
keystringYesThe list key.
startPosintYesStart index (inclusive).
stopPosintYesStop index (inclusive).

Returns: string|redis:Error

Sample code:

string result = check redis->lTrim("tasks", 0, 9);

Sample response:

"OK"
rPopLPush

Remove the last element in a list, prepend it to another list, and return it.

Parameters:

NameTypeRequiredDescription
srcstringYesThe source list key.
destinationstringYesThe destination list key.

Returns: string|redis:Error

Sample code:

string moved = check redis->rPopLPush("processing", "completed");

Sample response:

"task1"

Set operations

sAdd

Add one or more members to a set.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.
valuesstring[]YesThe members to add.

Returns: int|redis:Error

Sample code:

int added = check redis->sAdd("tags", ["ballerina", "redis", "integration"]);

Sample response:

3
sMembers

Get all the members in a set.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.

Returns: string[]|redis:Error

Sample code:

string[] members = check redis->sMembers("tags");

Sample response:

["ballerina", "redis", "integration"]
sCard

Get the number of members in a set.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.

Returns: int|redis:Error

Sample code:

int count = check redis->sCard("tags");

Sample response:

3
sIsMember

Determine if a given value is a member of a set.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.
valuestringYesThe value to check.

Returns: boolean|redis:Error

Sample code:

boolean isMember = check redis->sIsMember("tags", "redis");

Sample response:

true
sRem

Remove one or more members from a set.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.
membersstring[]YesThe members to remove.

Returns: int|redis:Error

Sample code:

int removed = check redis->sRem("tags", ["integration"]);

Sample response:

1
sPop

Remove and return one or more random members from a set.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.
countintYesNumber of members to pop.

Returns: string[]|redis:Error?

Sample code:

string[]? popped = check redis->sPop("tags", 1);

Sample response:

["redis"]
sRandMember

Get one or multiple random members from a set without removing them.

Parameters:

NameTypeRequiredDescription
keystringYesThe set key.
countintYesNumber of random members to return.

Returns: string[]|redis:Error

Sample code:

string[] randomMembers = check redis->sRandMember("tags", 2);

Sample response:

["ballerina", "redis"]
sMove

Move a member from one set to another.

Parameters:

NameTypeRequiredDescription
srcstringYesThe source set key.
destinationstringYesThe destination set key.
memberstringYesThe member to move.

Returns: boolean|redis:Error

Sample code:

boolean moved = check redis->sMove("active_tags", "archived_tags", "old_tag");

Sample response:

true
sDiff

Return the set resulting from the difference between the first set and all successive sets.

Parameters:

NameTypeRequiredDescription
keysstring[]YesThe set keys.

Returns: string[]|redis:Error

Sample code:

string[] diff = check redis->sDiff(["set1", "set2"]);

Sample response:

["unique_to_set1"]
sDiffStore

Store the difference between the first set and all successive sets at a destination key.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe set keys.

Returns: int|redis:Error

Sample code:

int count = check redis->sDiffStore("diff_result", ["set1", "set2"]);

Sample response:

1
sInter

Return the set resulting from the intersection of all provided sets.

Parameters:

NameTypeRequiredDescription
keysstring[]YesThe set keys.

Returns: string[]|redis:Error

Sample code:

string[] common = check redis->sInter(["set1", "set2"]);

Sample response:

["common_member"]
sInterStore

Store the intersection of all provided sets at a destination key.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe set keys.

Returns: int|redis:Error

Sample code:

int count = check redis->sInterStore("inter_result", ["set1", "set2"]);

Sample response:

1
sUnion

Return the set resulting from the union of all provided sets.

Parameters:

NameTypeRequiredDescription
keysstring[]YesThe set keys.

Returns: string[]|redis:Error

Sample code:

string[] all = check redis->sUnion(["set1", "set2"]);

Sample response:

["member1", "member2", "member3"]
sUnionStore

Store the union of all provided sets at a destination key.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe set keys.

Returns: int|redis:Error

Sample code:

int count = check redis->sUnionStore("union_result", ["set1", "set2"]);

Sample response:

3

Sorted set operations

zAdd

Add one or more members to a sorted set, or update the score of an existing member.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
memberScoreMapmap<any>YesA map of member names to their scores.

Returns: int|redis:Error

Sample code:

int added = check redis->zAdd("leaderboard", {
"alice": 100.0,
"bob": 85.0,
"charlie": 92.0
});

Sample response:

3
zRange

Return a range of members in a sorted set by index (low to high).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minintYesStart index (inclusive).
maxintYesStop index (inclusive).

Returns: string[]|redis:Error

Sample code:

string[] members = check redis->zRange("leaderboard", 0, -1);

Sample response:

["bob", "charlie", "alice"]
zRevRange

Return a range of members in a sorted set by index (high to low).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minintYesStart index (inclusive).
maxintYesStop index (inclusive).

Returns: string[]|redis:Error

Sample code:

string[] topPlayers = check redis->zRevRange("leaderboard", 0, 2);

Sample response:

["alice", "charlie", "bob"]
zRangeByScore

Return members in a sorted set with scores within the given range (low to high).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minfloatYesMinimum score (inclusive).
maxfloatYesMaximum score (inclusive).

Returns: string[]|redis:Error

Sample code:

string[] members = check redis->zRangeByScore("leaderboard", 90.0, 100.0);

Sample response:

["charlie", "alice"]
zRevRangeByScore

Return members in a sorted set with scores within the given range (high to low).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minfloatYesMinimum score (inclusive).
maxfloatYesMaximum score (inclusive).

Returns: string[]|redis:Error

Sample code:

string[] members = check redis->zRevRangeByScore("leaderboard", 80.0, 100.0);

Sample response:

["alice", "charlie", "bob"]
zRangeByLex

Return a range of members in a sorted set by lexicographical range (lowest to highest).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minstringYesMinimum lex bound (e.g., [a or -).
maxstringYesMaximum lex bound (e.g., [z or +).

Returns: string[]|redis:Error

Sample code:

string[] members = check redis->zRangeByLex("names", "-", "+");

Sample response:

["alice", "bob", "charlie"]
zRevRangeByLex

Return a range of members in a sorted set by lexicographical range (highest to lowest).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minstringYesMinimum lex bound.
maxstringYesMaximum lex bound.

Returns: string[]|redis:Error

Sample code:

string[] members = check redis->zRevRangeByLex("names", "+", "-");

Sample response:

["charlie", "bob", "alice"]
zScore

Get the score associated with the given member in a sorted set.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
memberstringYesThe member name.

Returns: float|redis:Error

Sample code:

float score = check redis->zScore("leaderboard", "alice");

Sample response:

100.0
zRank

Determine the index of a member in a sorted set (lowest score = rank 0).

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
memberstringYesThe member name.

Returns: int|redis:Error

Sample code:

int rank = check redis->zRank("leaderboard", "alice");

Sample response:

2
zRevRank

Determine the index of a member in a sorted set, with scores ordered from high to low.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
memberstringYesThe member name.

Returns: int|redis:Error

Sample code:

int rank = check redis->zRevRank("leaderboard", "alice");

Sample response:

0
zCard

Get the number of members in a sorted set.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.

Returns: int|redis:Error

Sample code:

int count = check redis->zCard("leaderboard");

Sample response:

3
zCount

Count the members in a sorted set with scores within the given range.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minfloatYesMinimum score (inclusive).
maxfloatYesMaximum score (inclusive).

Returns: int|redis:Error

Sample code:

int count = check redis->zCount("leaderboard", 90.0, 100.0);

Sample response:

2
zLexCount

Count the number of members in a sorted set within a lexicographical range.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minstringYesMinimum lex bound.
maxstringYesMaximum lex bound.

Returns: int|redis:Error

Sample code:

int count = check redis->zLexCount("names", "-", "+");

Sample response:

3
zIncrBy

Increment the score of a member in a sorted set.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
amountfloatYesThe increment amount.
memberstringYesThe member name.

Returns: float|redis:Error

Sample code:

float newScore = check redis->zIncrBy("leaderboard", 10.0, "bob");

Sample response:

95.0
zRem

Remove one or more members from a sorted set.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
membersstring[]YesThe members to remove.

Returns: int|redis:Error

Sample code:

int removed = check redis->zRem("leaderboard", ["bob"]);

Sample response:

1
zRemRangeByRank

Remove all members in a sorted set within the given indices.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minintYesStart index.
maxintYesStop index.

Returns: int|redis:Error

Sample code:

int removed = check redis->zRemRangeByRank("leaderboard", 0, 1);

Sample response:

2
zRemRangeByScore

Remove all members in a sorted set within the given scores.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minfloatYesMinimum score.
maxfloatYesMaximum score.

Returns: int|redis:Error

Sample code:

int removed = check redis->zRemRangeByScore("leaderboard", 0.0, 50.0);

Sample response:

0
zRemRangeByLex

Remove all members in a sorted set within the given lexicographical range.

Parameters:

NameTypeRequiredDescription
keystringYesThe sorted set key.
minstringYesMinimum lex bound.
maxstringYesMaximum lex bound.

Returns: int|redis:Error

Sample code:

int removed = check redis->zRemRangeByLex("names", "[a", "[b");

Sample response:

1
zInterStore

Intersect multiple sorted sets and store the resulting sorted set in a new key.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe sorted set keys to intersect.

Returns: int|redis:Error

Sample code:

int count = check redis->zInterStore("common_scores", ["set1", "set2"]);

Sample response:

2
zUnionStore

Compute the union of multiple sorted sets and store the result in a new key.

Parameters:

NameTypeRequiredDescription
destinationstringYesThe destination key.
keysstring[]YesThe sorted set keys to union.

Returns: int|redis:Error

Sample code:

int count = check redis->zUnionStore("all_scores", ["set1", "set2"]);

Sample response:

5

Hash operations

hSet

Set the string value of a hash field.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.
valuestringYesThe field value.

Returns: boolean|redis:Error

Sample code:

boolean isNew = check redis->hSet("user:1001", "name", "John Doe");

Sample response:

true
hGet

Get the value of a hash field.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.

Returns: string|redis:Error

Sample code:

string name = check redis->hGet("user:1001", "name");

Sample response:

"John Doe"
hGetAll

Get all the fields and values in a hash.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.

Returns: map<any>|redis:Error

Sample code:

map<any> user = check redis->hGetAll("user:1001");

Sample response:

{"name": "John Doe", "email": "[email protected]", "age": "30"}
hMSet

Set multiple hash fields to multiple values.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldValueMapmap<any>YesA map of field names to values.

Returns: string|redis:Error

Sample code:

string result = check redis->hMSet("user:1001", {
"name": "John Doe",
"email": "[email protected]",
"age": "30"
});

Sample response:

"OK"
hMGet

Get the values of all the given hash fields.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldsstring[]YesThe field names to retrieve.

Returns: map<any>|redis:Error

Sample code:

map<any> values = check redis->hMGet("user:1001", ["name", "email"]);

Sample response:

{"name": "John Doe", "email": "[email protected]"}
hDel

Delete one or more hash fields.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldsstring[]YesThe field names to delete.

Returns: int|redis:Error

Sample code:

int deleted = check redis->hDel("user:1001", ["age"]);

Sample response:

1
hExists

Determine if a hash field exists.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.

Returns: boolean|redis:Error

Sample code:

boolean exists = check redis->hExists("user:1001", "email");

Sample response:

true
hSetNx

Set the value of a hash field, only if the field does not exist.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.
valuestringYesThe field value.

Returns: boolean|redis:Error

Sample code:

boolean wasSet = check redis->hSetNx("user:1001", "role", "admin");

Sample response:

true
hKeys

Get all the fields in a hash.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.

Returns: string[]|redis:Error

Sample code:

string[] fields = check redis->hKeys("user:1001");

Sample response:

["name", "email", "age"]
hVals

Get all the values in a hash.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.

Returns: string[]|redis:Error

Sample code:

string[] values = check redis->hVals("user:1001");

Sample response:

["John Doe", "[email protected]", "30"]
hLen

Get the number of fields in a hash.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.

Returns: int|redis:Error

Sample code:

int fieldCount = check redis->hLen("user:1001");

Sample response:

3
hIncrBy

Increment the integer value of a hash field by the given number.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.
amountintYesThe increment amount.

Returns: int|redis:Error

Sample code:

int newAge = check redis->hIncrBy("user:1001", "age", 1);

Sample response:

31
hIncrByFloat

Increment the float value of a hash field by the given amount.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.
amountfloatYesThe float increment amount.

Returns: float|redis:Error

Sample code:

float newBalance = check redis->hIncrByFloat("account:1001", "balance", 25.50);

Sample response:

125.50
hStrLen

Get the string length of the value associated with a field in a hash.

Parameters:

NameTypeRequiredDescription
keystringYesThe hash key.
fieldstringYesThe field name.

Returns: int|redis:Error

Sample code:

int length = check redis->hStrLen("user:1001", "name");

Sample response:

8

Key operations

del

Delete one or more keys.

Parameters:

NameTypeRequiredDescription
keysstring[]YesThe keys to delete.

Returns: int|redis:Error

Sample code:

int deleted = check redis->del(["key1", "key2", "key3"]);

Sample response:

3
exists

Determine how many of the specified keys exist.

Parameters:

NameTypeRequiredDescription
keysstring[]YesThe keys to check.

Returns: int|redis:Error

Sample code:

int count = check redis->exists(["name", "counter", "nonexistent"]);

Sample response:

2
expire

Set a key's time to live in seconds.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.
secondsintYesExpiration time in seconds.

Returns: boolean|redis:Error

Sample code:

boolean result = check redis->expire("session:abc", 3600);

Sample response:

true
pExpire

Set a key's time to live in milliseconds.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.
expirationTimeintYesExpiration time in milliseconds.

Returns: boolean|redis:Error

Sample code:

boolean result = check redis->pExpire("temp:data", 5000);

Sample response:

true
ttl

Get the time to live for a key in seconds.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.

Returns: int|redis:Error

Sample code:

int remainingSeconds = check redis->ttl("session:abc");

Sample response:

3542
pTtl

Get the time to live for a key in milliseconds.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.

Returns: int|redis:Error

Sample code:

int remainingMs = check redis->pTtl("temp:data");

Sample response:

4320
persist

Remove the expiration from a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key.

Returns: boolean|redis:Error

Sample code:

boolean result = check redis->persist("session:abc");

Sample response:

true
keys

Find all keys matching the given pattern.

Parameters:

NameTypeRequiredDescription
patternstringYesThe glob-style pattern to match (e.g., user:*).

Returns: string[]|redis:Error

Sample code:

string[] matchingKeys = check redis->keys("user:*");

Sample response:

["user:1001", "user:1002", "user:1003"]
rename

Rename a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe current key name.
newNamestringYesThe new key name.

Returns: string|redis:Error

Sample code:

string result = check redis->rename("old_key", "new_key");

Sample response:

"OK"
renameNx

Rename a key, only if the new key does not exist.

Parameters:

NameTypeRequiredDescription
keystringYesThe current key name.
newNamestringYesThe new key name.

Returns: boolean|redis:Error

Sample code:

boolean renamed = check redis->renameNx("old_key", "new_key");

Sample response:

true
move

Move a key to another database.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to move.
databaseintYesThe target database index.

Returns: boolean|redis:Error

Sample code:

boolean moved = check redis->move("temp_key", 1);

Sample response:

true
sort

Sort the elements in a list, set, or sorted set.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to sort.

Returns: string[]|redis:Error

Sample code:

string[] sorted = check redis->sort("numbers");

Sample response:

["1", "2", "3", "5", "8"]
randomKey

Return a random key from the keyspace.

Returns: string|redis:Error?

Sample code:

string? key = check redis->randomKey();

Sample response:

"user:1002"
redisType

Determine the type stored at a key.

Parameters:

NameTypeRequiredDescription
keystringYesThe key to inspect.

Returns: string|redis:Error

Sample code:

string keyType = check redis->redisType("user:1001");

Sample response:

"hash"

Connection operations

ping

Ping the server to test connectivity.

Returns: string|redis:Error

Sample code:

string response = check redis->ping();

Sample response:

"PONG"
auth

Authenticate to the server.

Parameters:

NameTypeRequiredDescription
passwordstringYesThe authentication password.

Returns: string|redis:Error

Sample code:

string result = check redis->auth("my_password");

Sample response:

"OK"
echo

Echo the given string back from the server.

Parameters:

NameTypeRequiredDescription
messagestringYesThe message to echo.

Returns: string|redis:Error

Sample code:

string response = check redis->echo("Hello Redis");

Sample response:

"Hello Redis"
close

Close the connection to the Redis server.

Returns: redis:Error?

Sample code:

check redis.close();

Cluster operations

clusterInfo

Retrieve information and statistics about the Redis cluster.

Returns: string[]|redis:Error

Sample code:

string[] info = check redis->clusterInfo();

Sample response:

["cluster_enabled:1", "cluster_state:ok", "cluster_slots_assigned:16384", "cluster_slots_ok:16384", "cluster_known_nodes:6", "cluster_size:3"]