Updated on 2025-09-02 GMT+08:00

Connecting to Redis on StackExchange.Redis (C#)

This section describes how to access a Redis instance on StackExchange.Redis. For more information about how to use other Redis clients, visit the Redis official website.

The following operations are based on an example of accessing a Redis instance on a client on an elastic cloud server (ECS).

Notes and Constraints

If you use the StackExchange client to connect to a Proxy Cluster instance, the multi-DB function cannot be used.

To access a Redis 7.0 instance, use a hiredis 2.6.111 or later client. 2.7.0 and later versions are recommended.

Prerequisites

  • A Redis instance is created, and is in the Running state. To create a Redis instance, see Buying a DCS Redis Instance.
  • An ECS has been created. For details about how to create an ECS, see Purchasing a Custom ECS
  • The Linux ECS must have GNU Compiler Collection (GCC) installed. To query the GCC version, run the gcc --version command.

    Run the following command to install GCC on the ECS if needed:

    yum install -y make
    yum install -y pcre-devel
    yum install -y zlib-devel
    yum install -y libevent-devel
    yum install -y openssl-devel
    yum install -y gcc-c++
  • The client and the Redis instance must be interconnected before connecting to the instance. For details, see Network Conditions for Accessing DCS Redis.

Connecting to Redis on StackExchange.Redis

  1. View the IP address/domain name and port of the DCS Redis instance to be accessed.

    For details, see Viewing and Modifying Basic Settings of a DCS Instance.

  2. Log in to the ECS.

    A Windows ECS is used as an example.

  3. Install Visual Studio Community 2017 on the ECS.
  4. Start Visual Studio 2017 and create a project.

    Set the project name to redisdemo.

  5. Install StackExchange.Redis by using the NuGet package manager of Visual Studio.

    Access the NuGet package manager console according to Figure 1, and enter Install-Package StackExchange.Redis -Version 2.2.79. (The version number is optional).

    Figure 1 Accessing the NuGet package manager console

  6. Write the following code, and use the String Set and Get methods to test the connection.

    using System;
    using StackExchange.Redis;
    
    namespace redisdemo
    {
        class Program
        {
            // redis config
            private static ConfigurationOptions connDCS = ConfigurationOptions.Parse("{instance_ip_address}:{port},password=********,connectTimeout=2000");
            //the lock for singleton
            private static readonly object Locker = new object();
            //singleton
            private static ConnectionMultiplexer redisConn;
            //singleton
            public static ConnectionMultiplexer getRedisConn()
            {
                if (redisConn == null)
                {
                    lock (Locker)
                    {
                        if (redisConn == null || !redisConn.IsConnected)
                        {
                            redisConn = ConnectionMultiplexer.Connect(connDCS);
                        }
                    }
                }
                return redisConn;
            }
            static void Main(string[] args)
            {
                redisConn = getRedisConn();
                var db = redisConn.GetDatabase();
                //set get
                string strKey = "Hello";
                string strValue = "DCS for Redis!";
                Console.WriteLine( strKey + ", " + db.StringGet(strKey));
    
                Console.ReadLine();
            }
        }
    }

    {instance_ip_address} and {port} are the IP address/domain name and port of the DCS Redis instance. For details about how to obtain the IP address/domain name and port, see 1. Change them as required. ******** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. To connect to an instance using an ACL user, configure the instance password to username:password. For details about how to create or view an ACL user, see Configuring DCS Redis ACL Users. Omit the password setting in the command for a password-free instance.

  7. Run the code. You have successfully accessed the instance if the following command output is displayed:

    Hello, DCS for Redis!

    For more information about other commands of StackExchange.Redis, visit StackExchange.Redis.

Related Document

When accessing Redis fails, see Troubleshooting Redis Connection Failures.