Recently, due to the need, we have provided redis connection examples in various languages for Xiaobai customers. It includes C ා (StackExchange.Redis).
Don't talk too much Huawei cloud distributed cache service (for Redis) for example, share the C ා client connection configuration process of Redis.
1. First, I bought a Redis cache instance on Huawei cloud. At the same time, I bought an ECS server and chose Windows Server 2012.
2. Install VS 2017 Community Edition on ECS, because the Community Edition is free:)
3. Launch VS 2017 and create a new project. I use "redisdemo" for the project name
4. Install StackExchange.Redis with nuget management tool
In the nuget console, enter: install package stackexchange.redis - Version 1.2.6
Version number can not be specified.
5. Write the following code, and use the set and get of String to test the connection. See the code at the end.
6. Run the code. The output of the console interface is as follows, indicating that the connection is successful.
7. For other commands of the client, please refer to the official website: https://stackexchange.github.io/StackExchange.Redis/
using System; using StackExchange.Redis; namespace redisdemo { class Program { // redis config private static ConfigurationOptions connDCS = ConfigurationOptions.Parse("198.19.38.233:6379,password=Heru+123,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(); } } }