.Net Connection Example

Last updated: 2019-09-09 12:18:54

PDF

Preparations before running:
Download and install ServiceStack.Redis.

Sample code:

  • Do not use connection pool
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using ServiceStack.Redis; 
using System;

namespace ConsoleApplication1 
{ 
    class Program 
    { 
        static void Main(string[] args) 
       { 
           string host = "10.66.82.46";// Instance access host address 
           int port = 6379;// Port information 
           string instanceId = "bd87dadc-84f1-44f1-86dd-021dc4acde96";// Instance ID 
           string pass = "1234567q";// Password 

           RedisClient redisClient = new RedisClient(host, port, instanceId + ":" + pass); 
           string key = "name"; 
           string value = "QcloudV5!"; 
           redisClient.Set(key, value); // Set value 
           System.Console.WriteLine("set key:[" + key + "]value:[" + value + "]"); 
           string getValue = System.Text.Encoding.Default.GetString(redisClient.Get(key)); // Read value 
           System.Console.WriteLine("value:" + getValue); 
           System.Console.Read(); 
          } 
     } 
}
  • Use ServiceStack 4.0 connection pool
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using ServiceStack.Redis; 
using System;

namespace ConsoleApplication2 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
             string[] testReadWriteHosts = new[] {
             "redis://:fb92bf2e0abf11e5:1234561178a1A@10.0.0.1:6379"/*redis://:instance ID:password@access address:port*/
             };
             RedisConfig.VerifyMasterConnections = false;// Need to be set
             PooledRedisClientManager redisPoolManager = new PooledRedisClientManager(10/*connection pool quantity*/, 
             10/*connection pool timeout period*/, testReadWriteHosts);
             for (int i = 0; i < 100; i++)
            {
                 IRedisClient redisClient = redisPoolManager.GetClient();// Get the connection
                 RedisNativeClient redisNativeClient = (RedisNativeClient)redisClient;
                 redisNativeClient.Client = null;// Need to be set
                 try
                {
                   string key = "test1111";
                   string value = "test1111";
                   redisClient.Set(key, value);
                   redisClient.Dispose();//
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message);
                }
            }
            System.Console.Read();
         } 
     } 
}
  • Use ServiceStack 3.0 connection pool
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using ServiceStack.Redis; 
using System;

namespace ConsoleApplication3 
{ 
  class Program 
  { 
     static void Main(string[] args) 
    { 
           string[] testReadWriteHosts = new[] {
               "fb92bf2e0abf11e5:1234561178a1A@10.0.0.1:6379" /*instance ID:password@access address:port*/
               };
               PooledRedisClientManager redisPoolManager = new PooledRedisClientManager(10/*connection pool
               quantity*/, 10/*connection pool timeout period*/, testReadWriteHosts);
               for (int i = 0; i < 100; i++)
              {
               IRedisClient redisClient = redisPoolManager.GetClient();// Get the connection
               try
              {
                  string key = "test1111";
                  string value = "test1111";
                  redisClient.Set(key, value);
                  redisClient.Dispose();//
              } 
              catch (Exception e)
             {
                   System.Console.WriteLine(e.Message);
             }
          }
          System.Console.Read();
      } 
   } 
}

Execution result: