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).
Prerequisites
- A DCS Redis instance has been created and is in the Running state.
- An ECS has been created. For details about how to create an ECS, 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.
Connecting to Redis on StackExchange.Redis
- View the IP address and port number of the DCS Redis instance to be accessed.
For details, see Viewing Details of a DCS Instance.
- Log in to the ECS.
A Windows ECS is used as an example.
- Install Visual Studio Community 2017 on the ECS.
- Start Visual Studio 2017 and create a project.
Set the project name to redisdemo.
- 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).
- 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 and port number of the DCS Redis instance. For details about how to obtain the IP address 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.
- 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.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot