## Redis Client Tutorial

This tutorial provides information about key-value NoSQL type technology.

[Redis](https://redis.io/) 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 and it is a required component of the SDK. 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 the `AEA.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:

```json
{
    "Name": "Sender",
    "AEA2": {
       "LogLevel": "Trace", 
        "RedisClient": {
            "Server": "localhost",
            "Port": 6379,
        }
    }
}
```

{% admonition type="info" %}
If the Redis server is temporarily unreachable at startup or an existing connection is lost during runtime, 
the SDK will attempt to reconnect to the redis server using an exponential backoff retry mechanism. 
Among the Redis errors that will trigger a retry are `ConnectionError`, `TimeoutError`, `ConnectionResetError` and `ConnectionAbortedError`
{% /admonition %}

**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:**

# [NET](#tab/net)

* `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](https://stackexchange.github.io/StackExchange.Redis/).


* `public void Dispose()` - Cleans up resources required to keep the connection live to the Redis Server. Dispose makes underneath connection closed.


# [Python](#tab/python)

* `def connect(self, sec: float):` - 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 Py Package [Redis-Server](https://pypi.org/project/redis-server/).

* `def dispose(self):` - 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:

```command
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:


```json
{
    "Name": "Sender",
    "AEA2": {
       "LogLevel": "Trace", 
        "RedisClient": {
            "Server": "localhost",
            "Port": 6379,
        }
    }
}
```
# [NET](#tab/net)

### Create a Simple Console .NET Application

#### Programe.cs

```csharp
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");
        }
    }
}
```

# [Python](#tab/python)

### Create a Simple Console Application in Python

```python
from agoraiot import *

redisclient = redisClientSingleton.connect(2)
```
---

On running the app locally, Application should connect and print the below output.

![RedisClient](images/RedisClient.png)