redis
Module redis
API
ballerinax/redis Ballerina library
Overview
The Ballerina connector for Redis allows you to connect to Redis and manipulate key-value data stored in a Redis database. You can use the connector to manipulate string, hash, list, set, and sorted set data types. The functionality of the Ballerina connector for Redis is limited to the use of Redis as a database and cache. The message broker functionality of Redis is not yet exposed through this module.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Make sure Redis cache is running in the local environment or use Redis cache running on the cloud (for example, Redis Enterprise Cloud).
- Configure the connector with details related to redis cache such as hostname, port number and password.
Quickstart
To use the Redis connector in your Ballerina application, update the .bal file as follows:
Step 1: Import connector
Import the ballerinax/redis
module into the Ballerina project as follows..
import ballerinax/redis;
Step 2: Create a new connector instance
To access a Redis datasource, you must first create a client
object. Create a client of the redis client type (i.e. redis:Client
) and provide the necessary connection parameters. This creates a connection/pool of connections to the given Redis database. A method for creating a client with a Redis client can be found below.
redis:ConnectionConfig redisConfig = { host: "localhost", password: "", options: { connectionPooling: true, isClusterConnection: false, ssl: false, startTls: false, verifyPeer: false, connectionTimeout: 500 } }; redis:Client conn = check new (redisConfig);
Step 3: Invoke connector operation
- You can insert a value for a specific key as follows.
string stringSetresult = check connection->set("Project", "Ballerina");
- Use
bal run
command to compile and run the Ballerina program.
Functions
mGet
mSet
mSetNx
Clients
redis: Client
Ballerina Redis connector provides the capability to access Redis cache. This connector lets you to perform operations to access and manipulate key-value data stored in a Redis database.
Constructor
Initializes the connector. During initialization you have to pass needed configurations such as host name, port number and password.
init (ConnectionConfig config)
- config ConnectionConfig - Configuration for the connector
append
Append a value to a key.
bitCount
Count set bits in a string.
Parameters
- key string - Key referring to a value
bitOpAnd
Perform bitwise AND between strings.
Parameters
- destination string - Result key of the operation
- keys string[] - Input keys to perform AND between
Return Type
bitOpOr
Perform bitwise OR between strings.
Parameters
- destination string - Result key of the operation
- keys string[] - Input keys to perform OR between
Return Type
bitOpNot
Perform bitwise NOT on a string.
bitOpXor
Perform bitwise XOR between strings.
Parameters
- destination string - Result key of the operation
- keys string[] - Input keys to perform XOR between
Return Type
decr
Decrement integer value of a key by one.
Parameters
- key string - Key referring to a value
decrBy
Decrement integer value of a key by the given number.
getBit
Returns bit value at offset in the string value stored at key.
getRange
Get substring of string stored at a key.
Parameters
- key string - Key referring to a value
- startPos int - Starting point of substring
- end int - End point of substring
getSet
Set string value of key and return its existing value.
Return Type
get
Get value of key.
Parameters
- key string - Key referring to a value
incr
Increment integer value of a key by one.
Parameters
- key string - Key referring to a value
incrBy
Increment integer value of key by the given amount.
incrByFloat
Increment integer value of key by the given float.
mGet
Get values of all given keys.
Parameters
- keys string[] - Keys of which values need to be retrieved
mSet
Set multiple keys to multiple values.
Parameters
- keyValueMap map<any> - Map of key-value pairs to be set
mSetNx
Set multiple keys to multiple values, only if none of the keys exist.
Parameters
- keyValueMap map<any> - Map of key-value pairs to be set
pSetEx
Set value and expiration in milliseconds of a key.
Parameters
- key string - Key referring to a value
- value string - Value to be set
- expirationTime int - Expiration time in milli seconds
set
Set the value of a key.
setBit
Sets or clears the bit at offset in the string value stored at key.
Parameters
- key string - Key referring to a value
- value int - Value to be set
- offset int - Offset at which the value should be set
setEx
Set the value and expiration of a key.
Parameters
- key string - Key referring to a value
- value string - Value to be set
- expirationPeriodSeconds int - Expiration time to be set, in seconds
setNx
Set value of a key, only if key does not exist.
setRange
Overwrite part of string at key starting at the specified offset.
Parameters
- key string - Key referring to a value
- offset int - Offset at which the value should be set
- value string - Value to be set
strln
Get length of value stored in a key.
Parameters
- key string - Key referring to a value
Return Type
lPush
Prepend one or multiple values to list.
lPop
Remove and get the first element in a list.
Parameters
- key string - Key referring to a value
Return Type
lPushX
Prepend one or multiple values to a list, only if the list exists.
bLPop
Remove and get the first element in a list, or block until one is available.
Return Type
bRPop
Remove and get the last element in a list, or block until one is available.
Return Type
lIndex
Get an element from list by its index.
Parameters
- key string - Key referring to a value
- index int - Index from which the element should be retrieved
lInsert
Insert an element before or after another element in a list.
Parameters
- key string - Key referring to a value
- before boolean - Boolean value representing Whether element should be inserted before or after the pivot
- pivot string - Pivot position
- value string - Value to insert
Return Type
lLen
Get length of a list.
Parameters
- key string - Key referring to a value
lRange
Get a range of elements from a list.
Parameters
- key string - Key referring to a value
- startPos int - Begining index of the range
- stopPos int - Last index of the range
lRem
Remove elements from list.
Parameters
- key string - Key referring to a value
- count int - Number of elements to be removed
- value string - Value which the elements to be removed should be equal to
lSet
Set the value of an element in a list by its index.
Parameters
- key string - Key of the list
- index int - Index of the element of which the value needs to be set
- value string - Value to be set
Return Type
lTrim
Trim list to the specified range.
Parameters
- key string - Key of the list
- startPos int - Starting index of the range
- stopPos int - End index of the range
rPop
Remove and get the last element in a list.
Parameters
- key string - Key of the list
Return Type
rPopLPush
Remove the last element in a list, append it to another list and return it.
rPush
Append one or multiple values to a list.
rPushX
Append one or multiple values to a list, only if the list exists.
sAdd
Add one or more members to a set.
Return Type
sCard
Get the number of members in a set
Parameters
- key string - Key of the set
sDiff
Return set resulting from the difference between the first set and all the successive sets
Parameters
- keys string[] - The keys of the sets
sDiffStore
Obtain the set resulting from the difference between the first set and all the successive. sets and store at the provided destination.
Parameters
- destination string - Destination key of the resulting set
- keys string[] - Keys of the sets to find the difference of
sInter
Return the intersection of the provided sets.
Parameters
- keys string[] - Keys of the sets to be intersected
sInterStore
Obtain the intersection of the provided sets and store at the provided destination.
Parameters
- destination string - Destination key of the resulting set
- keys string[] - Keys of the sets to be intersected
sIsMember
Determine if a given value is a member of a set.
Return Type
sMembers
Get all members in a set.
Parameters
- key string - Key of the set
sMove
Move a member from one set to another.
Parameters
- src string - Source key
- destination string - Destination key
- member string - Member to be moved
Return Type
sPop
Remove and return a random member from a set.
Return Type
sRandMember
Get one or multiple random members from a set.
Return Type
sRem
Remove one or more members from a set.
Return Type
sUnion
Return the union of multiple sets.
Parameters
- keys string[] - Array of keys of sets
sUnionStore
Return the union of multiple sets.
Parameters
- destination string - Destination key of the resulting set
- keys string[] - Array of keys of sets
zAdd
Add one or more members to a sorted set, or update its score if it already exist.
Parameters
- key string - Key of the sorted set
- memberScoreMap map<any> - Map of members and corresponding scores
Return Type
zCard
Get the number of members in a sorted set.
Parameters
- key string - Key of the sorted set
Return Type
zCount
Count the members in a sorted set with scores within the given range.
Parameters
- key string - Key of the sorted set
- min float - Minimum score of the range
- max float - Maximum score of the range
zIncrBy
Increment the score of a member in a sorted set.
Parameters
- key string - Key of the sorted set
- amount float - Amount to increment
- member string - Member whose score to be incremented
zInterStore
Intersect multiple sorted sets and store the resulting sorted set in a new key.
Parameters
- destination string - Destination key of the resulting sorted set
- keys string[] - Keys of the sorted sets to be intersected
zLexCount
Count the members in a sorted set within the given lexicographical range.
Parameters
- key string - Key of the sorted set
- min string - Minimum lexicographical value of the range
- max string - Maximum lexicographical value of the range
Return Type
zRange
Return a range of members in a sorted set, by index.
Parameters
- key string - Key of the sorted set
- min int - Minimum index of the range
- max int - Maximum index of the range
zRangeByLex
Return a range of members in a sorted set, by lexicographical range from lowest to highest.
Parameters
- key string - Key of the sorted set
- min string - Minimum lexicographical value of the range
- max string - Maximum lexicographical value of the range
Return Type
zRevRangeByLex
Return a range of members in a sorted set, by lexicographical range ordered from highest to lowest.
Parameters
- key string - Key of the sorted set
- min string - Lexicographical value of the range
- max string - Maximum lexicographical value of the range
Return Type
zRangeByScore
Return a range of members in a sorted set, by score from lowest to highest.
Parameters
- key string - Key of sorted set
- min float - Minimum score of range
- max float - Maximum score of range
Return Type
zRank
Determine index of a member in a sorted set.
Parameters
- key string - Key of the sorted set
- member string - Member of which the index needs to be obtained
zRem
Remove one or more members from a sorted set
Return Type
zRemRangeByLex
Remove all members in a sorted set between the given lexicographical range.
Parameters
- key string - Key of the sorted set
- min string - Minimum lexicographical value of the range
- max string - Maximum lexicographical value of the range
zRemRangeByRank
Remove all members in a sorted set within the given indices.
Parameters
- key string - Key of the sorted set
- min int - Minimum index of the range
- max int - Maximum index of the range
zRemRangeByScore
Remove all members in a sorted set within the given scores.
Parameters
- key string - Key of the sorted set
- min float - Minimum score of the range
- max float - Maximum score of the range
zRevRange
Return a range of members in a sorted set, by index, ordered highest to lowest.
Parameters
- key string - Key of the sorted set
- min int - Minimum index of the range
- max int - Maximum index of the range
Return Type
zRevRangeByScore
Return a range of members in a sorted set, by score from highest to lowest.
Parameters
- key string - Key of the sorted set
- min float - Minimum score of the range
- max float - Maximum score of the range
Return Type
zRevRank
Determine the index of a member in a sorted set
Parameters
- key string - Key of the sorted set
- member string - Member of which the index needs to be obtained
zScore
Determine the score of a member in a sorted set
Parameters
- key string - Key of the sorted set
- member string - Member of which the score needs to be obtained
zUnionStore
Return the union of multiple sorted sets
Parameters
- destination string - Destination key of the resulting set
- keys string[] - Array of keys of sorted sets
hDel
Delete one or more hash fields.
Return Type
hExists
Determine if a hash field exists.
Return Type
hGet
Get the value of a hash field.
hGetAll
Get the all values of a hash.
Parameters
- key string - Key of the hash
hIncrBy
Increment the integer value of a hash field by the given number.
hIncrByFloat
Increment the float value of a hash field by the given number.
hKeys
Get all the fields in a hash.
Parameters
- key string - Key of the hash
hLen
Get the number of fields in a hash.
Parameters
- key string - Key of the hash
hMGet
Get the values of all the given hash fields.
hMSet
Set multiple hash fields to multiple values.
Return Type
hSet
Set the string value of a hash field.
Return Type
hSetNx
Set the string value of a hash field, only if the field does not exist.
Return Type
hStrln
Get the string length of the field value in a hash.
Return Type
hVals
Get all the values in a hash.
Parameters
- key string - Key of the hash
Return Type
del
Delete one or more keys.
Parameters
- keys string[] - Key to be deleted
exists
Determine how many keys exist.
Parameters
- keys string[] - Keys of which existence to be found out
expire
Set a key's time to live in seconds.
Return Type
keys
Find all keys matching the given pattern.
Parameters
- pattern string - Pattern to match
move
Move a key to another database.
Return Type
persist
Remove the expiration from a key.
Parameters
- key string - Key of which expiry time should be removed
Return Type
pExpire
Set a key's time to live in milliseconds.
Parameters
- key string - Key of which expiry time should be removed
- timeMilliSeconds int - Expiry time in milli seconds
Return Type
pTtl
Get the time to live for a key in milliseconds.
Parameters
- key string - Key of which time-to-live should be obtained
randomKey
Return a random key from the keyspace.
Return Type
rename
Rename a key.
Return Type
renameNx
Rename a key, only if the new key does not exist.
Return Type
sort
Sort elements in a list, set or sorted set.
Parameters
- key string - Key of the data typeure to be sorted
Return Type
ttl
Get the time to live for a key.
Parameters
- key string - Key of which the time to live needs to be obtained
Return Type
redisType
Determine the type stored at key.
Parameters
- key string - Key of which the type needs to be obtained
ping
Ping the server.
auth
Authenticate to the server.
Parameters
- password string - Password to authenticate
Return Type
echo
Echo the given string.
Parameters
- message string - Message to be echo-ed
Return Type
stop
function stop()
Stops the registered service.
Records
redis: ConnectionConfig
The Client endpoint configuration for Redis databases.
Fields
- host string(default "localhost") - The host of the Redis database
- password string(default "") - Password for the database connection
- options Options(default {}) - Properties for the connection configuration
redis: Options
Connection options for Redis Client Endpoint.
Fields
- clientName string(default "") - The clientName of the connection
- connectionPooling boolean(default false) - Boolean value depending on whether the connection pooling is enabled or not
- isClusterConnection boolean(default false) - Whether to enable cluster connection or not
- ssl boolean(default false) - Boolean value depending on whether SSL is enabled or not
- startTls boolean(default false) - Boolean value depending on whether startTLS is enabled or not
- verifyPeer boolean(default false) - Boolean value depending on whether peer verification is enabled or not
- database int(default -1) - The database to be used with the connection
- connectionTimeout int(default -1) - The timeout value for the connection
Errors
redis: Error
Represents a redis generic error
Import
import ballerinax/redis;
Metadata
Released date: over 1 year ago
Version: 2.3.2
License: Apache-2.0
Compatibility
Platform: java11
Ballerina version: 2201.3.1
GraalVM compatible: Yes
Pull count
Total: 19525
Current verison: 705
Weekly downloads
Keywords
IT Operations/Databases
Cost/Freemium
Contributors