Redis Client Tutorial
This tutorial provides information about key-value NoSQL type technology.
Redis is a NoSQL key-value cache that stores information in a hash table format. It provides the possibilities to store different type of structured data like strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.
With Edge SK 2.0 RedisClient functionality, developers can easily use Redis Server deployed on the gateway as a part of the Hermes Core Release.
Namespace: Agora.Edge
The RedisClient is implemented as a singleton. This singleton exposes a full-fledged RedisClient to interact with the server to store, retrieve values from Radis Server etc.
RedisClient can be configured from within the AEA.json
as shown below.
Configuration Parameters:
Name (string)
- Used to set the RedisClient Name that identifies the client to the server. Default:Entry Assembly Name
. Although "optional", it is recommended to specify the "Name" setting in theAEA.json
configuration file.AEA2:RedisClient:Server (string)
: Server name hosting the Redis Server. Default:localhost
. Commonly, this is set to the name of the container running Redis Server when deployed in a container.AEA2:RedisClient:Port (uint)
: Server Port hosting the Redis Server. Default:6379
. Commonly, this is set as a container create option with the port exposing Redis Server when deployed as a container.
Example of AEA.json
configuration file:
{
"Name": "Sender",
"AEA2": {
"LogLevel": "Trace",
"RedisClient": {
"Server": "localhost",
"Port": 6379,
}
}
}
Properties:
Instance: Redis
- Used to access the singleton instance and is the same as using the Agora.SDK.RedisClient.Client
- Used to access the Redis Server.IsConnected: bool
-True
if connected to Redis Server.
Methods:
void Connect(int timeout_msec)
- Connects to the Redis Server using configuration settings. If the server is not available, it will wait for timeout_msec and then it will try to connect in background. This is possible due to underneath Nuget Package StackExchange.Redis.public void Dispose()
- Cleans up resources required to keep the connection live to the Redis Server. Dispose makes underneath connection closed.
For Local Development and Testing
To get started with Redis Stack using docker, first you need to select a docker image:
redis/redis-stack
- contains both Redis Stack server and RedisInsight. This container is best for local development because yu can use the embedded RedisInsight to visualize your data.
To start a Redis Stack container using the Redis Stack image, run the following command in your terminal:
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Add an AEA.json
file to project with below setting:
{
"Name": "Sender",
"AEA2": {
"LogLevel": "Trace",
"RedisClient": {
"Server": "localhost",
"Port": 6379,
}
}
}
Create a Simple Console .NET Application
Programe.cs
using System.Text.Json;
using static Agora.SDK;
internal class Program
{
private static void Main(string[] args)
{
"Starting".LogHeading();
Redis.Connect(2000);
if (Redis.IsConnected)
{
var redisValues = Redis.Client?.ListRange("Key");
}
}
}
On running the app locally, Application should connect and print the below output.