Using C# to Access an Instance

Access a DCS Redis instance through C# Client StackExchange.Redis on an ECS in the same VPC. For more information on how to use other Redis clients, visit the Redis official website.

Prerequisites

  • The DCS Redis instance you want to access is in the Running state.
  • An ECS has been created to serve as your Redis client. For more information on how to create ECSs, see the Elastic Cloud Server User Guide.
  • If the ECS runs the Linux OS, ensure that the GCC compilation environment has been installed on the ECS.

Procedure

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

    For details, see Viewing Instance Details.

  2. Log in to the ECS.

    For this example, the Windows OS is used on the ECS.

  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 1.2.6. (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("10.10.38.233:6379,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();
            }
        }
    }

    10.10.38.233:6379 contains an example IP address/domain name and port number of the DCS Redis instance. For details on how to obtain the IP address/domain name and port, see 1. Change the IP address/domain name and port as required. ******** indicates the password used for login to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.

  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.

Video Guide

Watch the following video about accessing a DCS Redis instance.

Accessing a DCS Instance