Access a DCS Redis instance through C# Client StackExchange.Redis on an ECS in the same VPC. For more information about how to use other Redis clients, visit the Redis official website.
For details, see Viewing Details of a DCS Instance.
A Windows ECS is used as an example.
Set the project name to redisdemo.
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).
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 about 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 logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.
Hello, DCS for Redis!
For more information about other commands of StackExchange.Redis, visit StackExchange.Redis.