国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c#實現redis客戶端(一)

2019-11-17 02:38:31
字體:
來源:轉載
供稿:網友

c#實現redis客戶端(一)

2015-01-12 08:55 by 蘑菇先生, ... 閱讀, ... 評論, 收藏, 編輯

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

閱讀目錄:

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

協議規范

redis允許客戶端以TCP方式連接,默認6379端口。傳輸數據都以/r/n結尾。

請求格式

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

例:*1/r/n$4/r/nINFO/r/n

響應格式

1:簡單字符串,非二進制安全字符串,一般是狀態回復。 +開頭,例:+OK/r/n

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

3:整型數字。 :開頭, 例::1/r/n

4:大塊回復值,最大512M。 $開頭+數據長度。 例:$4/r/mush/r/n

5:多條回復。 *開頭, 例:*2/r/n$3/r/nfoo/r/n$3/r/nbar/r/n

基礎通信

定義配置類:

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("VEVb", "mushroom", 1000);        }

輸出:

*2 表示2條回復。

+2 表示命令執行OK。

:1 表示命令執行的結果

總結

本文只是簡單的實現,有興趣的同學,可以繼續下去。

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

參考資源:

http://redis.io/topics/protocol

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中方县| 隆安县| 广饶县| 秦安县| 虎林市| 杭锦后旗| 枣阳市| 福清市| 巫溪县| 达拉特旗| 三门县| 将乐县| 扎兰屯市| 漳浦县| 吉木萨尔县| 宁晋县| 广宗县| 盐边县| 定安县| 苍山县| 景德镇市| 宣化县| 和政县| 宜宾市| 喀喇沁旗| 丰都县| 探索| 和龙市| 衡东县| 枣阳市| 安溪县| 泸州市| 高要市| 荆门市| 闻喜县| 平山县| 呼玛县| 汶上县| 晴隆县| 泰来县| 金溪县|