程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Redis教程 >> 關於Redis >> Redis總結筆記(二):C#連接Redis簡單例子

Redis總結筆記(二):C#連接Redis簡單例子

編輯:關於Redis

注:C#在調用Redis是不要使用ServiceStack.Redis驅動的4.0版本,因為這個版本已經商業化了,會出現每小時6000條數據的限制

1、引用驅動
復制代碼 代碼如下:
    using ServiceStack.Redis;

2、數據庫連接
復制代碼 代碼如下:
    RedisClient client;
            //連接服務器   6379是redis的默認端口
            client = new RedisClient("127.0.0.1", 6379);
    client.Password = "";//設置密碼 沒有可以注釋

          //10萬條數據測試,我發現使用set的效率明顯比使用store的效率高,而且在測試過程中我發現store會丟失7-80條左右的數而set卻一條都沒有丟
          Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 100000; i++)
            {
                client.Set<GPS>(Guid.NewGuid().ToString(), new GPS
                {
                    direction = 287,
                    gps_time = "1417622213418",
                    lati = 29.310586,
                    longi = 120.125143,
                    pla_no = "浙A12345",
                    pla_type = 1,
                    speed = 23.5,
                    state = 0,
                    carstate = 0,
                    upload_time = "1417622088418"
                });

                client.Store<GPS>(
                    new GPS
                {
                    direction = 287,
                    gps_time = "1417622213418",
                    lati = 29.310586,
                    longi = 120.125143,
                    pla_no = "浙A12345",
                    pla_type = 1,
                    speed = 23.5,
                    state = 0,
                    carstate = 0,
                    upload_time = "1417622088418"
                });
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);

 decimal price = client.Get<decimal>("price");//獲取數據
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved