Module persist.redis
ballerinax/persist.redis Ballerina library
Overview
This module provides Redis database support for the bal persist
feature, which provides functionality to store and query data from a Redis database through a data model instead of writing Redis commands.
How to use with bal persist
Since Redis is not the default datastore for bal persist
you need to explicitly specify the data store in your application in either of the following ways,
Integrate to bal build
-
Initialize
bal persist
and integrate tobal build
using the following command,$ bal persist add --datastore redis --module <module_name>
-
After defining the entities, build the application using the following command,
$ bal build
One time generation
-
Initialize
bal persist
using the following command,$ bal persist init
-
Generate the persist client using the following command,
$ bal persist generate --datastore redis --module <module_name>
Supported Ballerina data types
The following table lists the Ballerina data types supported by the Redis data store. Following data types will be converted to string
when inserting data and converted back to relevent data types in ballerina when retrieving.
Ballerina Type |
---|
int |
float |
decimal |
string |
boolean |
time:Date |
time:TimeOfDay |
time:Civil |
time:Utc |
enum |
Configuration
You need to set a value for the following configuration parameter in the Config.toml
file in your project to use the Redis data store.
Parameter | Description |
---|---|
connection | The connection URI of the DB server. |
The following is a sample Config.toml
file with the Redis data store configuration. This is generated by the persist generate
command.
[<packageName>.<moduleName>.connectionConfig] connection = "redis://localhost:6379" [<packageName>.<moduleName>.cacheConfig] maxAge = -1
Alternatively, you can provide connection parameters instead of the URI
as follows.
[<packageName>.<moduleName>.connectionConfig.connection] host = "localhost" port = 6379
Currently, the Redis data store only supports global cache configuration at the client level. The maxAge
parameter in the cache configuration specifies the maximum age in seconds
for cached objects. A value of -1
indicates that objects within the cache have no expiry time, persisting indefinitely until manually removed.
Additionally, you can set values for the advanced configuration parameters in the Config.toml file in your project to use the Redis data store. For more information on these parameters, see the Redis Connector documentation.
Setup guide
Select one of the methods below to set up a Redis server.
Setup a Redis server locally
Install a Redis server on your machine locally by downloading and installing it based on your development platform. See the official Redis documentation.
Setup using docker
Use Docker to create a DB server deployment.
- Install Docker on your machine if you haven't already.
- Pull the Redis Docker image from Docker Hub using the command
docker pull redis
. - Run the Redis container as follows
docker run -d -p 6379:6379 --name <container-name> redis
.
Setup a cloud-based Redis service
Use a cloud-based DB solution such as Google’s Cloud, Amazon’s Web Services, or Microsoft’s Azure database.
-
Visit Redis cloud console.
-
Login using email and password or using one of the Single Sign-On options.
-
Select either Amazon Web Services, Google Cloud, or Microsoft Azure as the database vendor.
-
Select a region and create the database.
-
Find your
username
,password
and thepublic endpoint
-
Replace the
connection
parameter in theConfig.toml
file as belowconnection = "redis://: @ "