c#實現redis客戶端(一)

  最近項目使用中要改造redis客戶端,看了下文檔,總結分享一下。git

閱讀目錄:

  1. 協議規範
  2. 基礎通訊
  3. 狀態命令
  4. set、get命令
  5. 管道、事務
  6. 總結

協議規範

redis容許客戶端以TCP方式鏈接,默認6379端口。傳輸數據都以\r\n結尾。github

請求格式

*<number of arguments>\r\n$<number of bytes of argument 1>\r\n<argument data>\r\nredis

例:*1\r\n$4\r\nINFO\r\n算法

響應格式

1:簡單字符串,非二進制安全字符串,通常是狀態回覆。  +開頭,例:+OK\r\n 數組

2: 錯誤信息。          -開頭, 例:-ERR unknown command 'mush'\r\n安全

3: 整型數字。                            :開頭, 例::1\r\nsocket

4:大塊回覆值,最大512M。           $開頭+數據長度。 例:$4\r\mush\r\nui

5:多條回覆。                           *開頭, 例:*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\nthis

基礎通訊

定義配置類:spa

public class Configuration
    {
        public string Host { get; set; }
        public int Port { get; set; }
        /// <summary>
        /// Socket 是否正在使用 Nagle 算法。
        /// </summary>
        public bool NoDelaySocket { get; set; }

        public Configuration()
        {
            Host = "localhost";
            Port = 6379;
            NoDelaySocket = false;
        }
    }

實現socket鏈接:

 public class RedisBaseClient
    {
        //配置文件
        private Configuration configuration;
        //通訊socket
        private Socket socket;
        //接收字節數組
        private byte[] ReceiveBuffer = new byte[100000];

        public RedisBaseClient(Configuration config)
        {
            configuration = config;
        }

        public RedisBaseClient()
            : this(new Configuration())
        {
        }

        public void Connect()
        {
            if (socket != null && socket.Connected)
                return;
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = configuration.NoDelaySocket
            };
            socket.Connect(configuration.Host, configuration.Port);
            if (socket.Connected)
                return;
            Close();
        }

        /// <summary>
        /// 關閉client
        /// </summary>
        public void Close()
        {
            socket.Disconnect(false);
            socket.Close();
        }
    }

調用:

RedisBaseClient redis = new RedisBaseClient();
redis.Connect();

服務端成功響應:

  

狀態命令

定義Redis命令枚舉:

public enum RedisCommand
    {
        GET, //獲取一個key的值
        INFO, //Redis信息。  
        SET, //添加一個值
        EXPIRE, //設置過時時間
        MULTI, //標記一個事務塊開始
        EXEC, //執行全部 MULTI 以後發的命令
    }

發送命令構建:

  public string SendCommand(RedisCommand command, params string[] args)
        {
            //請求頭部格式, *<number of arguments>\r\n
            const string headstr = "*{0}\r\n";
            //參數信息       $<number of bytes of argument N>\r\n<argument data>\r\n
            const string bulkstr = "${0}\r\n{1}\r\n";

            var sb = new StringBuilder();
            sb.AppendFormat(headstr, args.Length + 1);

            var cmd = command.ToString();
            sb.AppendFormat(bulkstr, cmd.Length, cmd);

            foreach (var arg in args)
            {
                sb.AppendFormat(bulkstr, arg.Length, arg);
            }
            byte[] c = Encoding.UTF8.GetBytes(sb.ToString());
            try
            {
                Connect();
                socket.Send(c);

                socket.Receive(ReceiveBuffer);
                Close();
                return ReadData();
            }
            catch (SocketException e)
            {
                Close();
            }
            return null;
        }
   private string ReadData()
        {
            var data = Encoding.UTF8.GetString(ReceiveBuffer);
            char c = data[0];
            //錯誤消息檢查。
            if (c == '-') //異常處理。
                throw new Exception(data);
            //狀態回覆。
            if (c == '+')
                return data;
            return data;
        }

 調用:

 private void button1_Click(object sender, EventArgs e)
        {
            RedisBaseClient redis = new RedisBaseClient();
            var result = redis.SendCommand(RedisCommand.INFO);
            richTextBox1.Text = result;
        }

輸出響應,其$937是數據包的長度。

 

set、get命令

調用:

   private void button2_Click(object sender, EventArgs e)
        {
            RedisBaseClient redis = new RedisBaseClient();
            var result = redis.SendCommand(RedisCommand.SET, "msg", "testvalue");
            richTextBox1.Text = result.ToString();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            RedisBaseClient redis = new RedisBaseClient();
            var result = redis.SendCommand(RedisCommand.GET, "msg");
            richTextBox1.Text = result.ToString();
        }

輸出

管道、事務

 兩者都是走MULTI,EXEC命令,原子操做。管道就是發送命令(無需等上次命令回覆),進入命令隊列,而後多條命令一次執行,並返回客戶端結果。 

 日常使用ServiceStack.Redis客戶端都直接set了,實際上是set、expire 2個命令。 簡單實現以下:

        public void CreatePipeline()
        {
            SendCommand(RedisCommand.MULTI, new string[] {}, true);
        }
        public string EnqueueCommand(RedisCommand command, params string[] args)
        {
            return SendCommand(command, args, true);
        }
        public string FlushPipeline()
        {
            var result = SendCommand(RedisCommand.EXEC, new string[] {}, true);
            Close();
            return result;
        }
        public string SendCommand(RedisCommand command, string[] args, bool isPipeline=false)
        {
            //請求頭部格式, *<number of arguments>\r\n
            const string headstr = "*{0}\r\n";
            //參數信息       $<number of bytes of argument N>\r\n<argument data>\r\n
            const string bulkstr = "${0}\r\n{1}\r\n";

            var sb = new StringBuilder();
            sb.AppendFormat(headstr, args.Length + 1);

            var cmd = command.ToString();
            sb.AppendFormat(bulkstr, cmd.Length, cmd);

            foreach (var arg in args)
            {
                sb.AppendFormat(bulkstr, arg.Length, arg);
            }
            byte[] c = Encoding.UTF8.GetBytes(sb.ToString());
            try
            {
                Connect();
                socket.Send(c);
                
                socket.Receive(ReceiveBuffer);
                if (!isPipeline)
                {
                    Close();
                }
                return ReadData();
            }
            catch (SocketException e)
            {
                Close();
            }
            return null;
        }
        public string SetByPipeline(string key, string value, int second)
        {
            this.CreatePipeline();
            this.EnqueueCommand(RedisCommand.SET, key, value);
            this.EnqueueCommand(RedisCommand.EXPIRE, key, second.ToString());
            return this.FlushPipeline();
        }     

 調用:

  private void button4_Click(object sender, EventArgs e)
        {
            RedisBaseClient redis = new RedisBaseClient();
            richTextBox1.Text = redis.SetByPipeline("cnblogs", "mushroom", 1000);
        }

輸出:

*2 表示2條回覆。

+2 表示命令執行OK。

:1  表示命令執行的結果

總結

本文只是簡單的實現,有興趣的同窗,能夠繼續下去。

客戶端實現這塊,Socket鏈接池管理相較複雜些。

參考資源:

http://redis.io/topics/protocol

https://github.com/ServiceStack/ServiceStack.Redis

相關文章
相關標籤/搜索