Redis在.net中的應用學習

來源:互聯網
上載者:User

標籤:dll   evel   實體   collect   rom   tle   ubuntu   code   net   

在Redis的官網(http://redis.io/clients#c)上可以看到支援Redis C#的用戶端。

redis的網路連接方式和傳統的rdbms相似,一種是長串連,一種是串連池,此處使用長串連進行串連。

目前redis官方版本不支援.net直接進行串連,需要使用一些開源類庫。目前最流行的就是ServiceStack.redis,可以通過https://github.com/ServiceStack/ServiceStack.Redis下載最新版本。

下載完成解壓,在\ServiceStack.Redis-master\build\release\MonoDevelop目錄下看到ServiceStack.Redis.zip檔案,這個就是需要引入到.net項目中的4個dll檔案。

測試時發現四個檔案版本比較舊,可以通過編譯redis源碼,產生最新的dll檔案。

開啟VS2013,建立一個控制台應用程式,寫了一些簡單的Redis操作

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 using ServiceStack.Redis; 8 using ServiceStack.Redis.Support; 9 10 namespace RedisDemo11 {12     class Program13     {14         private static RedisClient redis = new RedisClient("192.168.32.216", 6379, "anny");15         static void Main(string[] args)16         {17             //單個字串寫入18             redis.SetValue("age", "20");19             //讀取指定key的字串20             redis.GetValue("age");21 22             //儲存數字23             redis.Set<int>("int_age", 30);24             int age = redis.Get<int>("int_age");25             Console.WriteLine("int_age={0}", age);26     27             //將字串列表寫入Redis List28             List<string> colourList = new List<string>{"red","pink","green","blue","black","white"};29             colourList.ForEach(item => redis.AddItemToList("colourList", item));30 31             //讀取Redis List內容32             List<string> colourList1 = redis.GetAllItemsFromList("colourList");33             colourList1.ForEach(item => Console.Write(item + " "));34 35             //儲存實體物件,在Redis中以json格式儲存36             UserInfo user = new UserInfo(){Id=1, Name="Mark", Age=32, City="ShangHai" };37             redis.Set<UserInfo>("user_1", user);38             UserInfo user1 = redis.Get<UserInfo>("user_1");39             Console.WriteLine("id={0},name={1},age={2},city={3}", user1.Id, user1.Name, user1.Age, user1.City);40 41             //object序列化方式42             var ser = new ObjectSerializer();43             redis.Set<byte[]>("user1", ser.Serialize(user));44             UserInfo user11 = ser.Deserialize(redis.Get<byte[]>("user1")) as UserInfo;45             Console.WriteLine("id={0},name={1},age={2},city={3}", user1.Id, user1.Name, user1.Age, user1.City);46 47             //儲存物件列表到redis中48             List<UserInfo> userList = new List<UserInfo>{49                 new UserInfo{Id=2, Name="Jack", Age=27, City="beijing" },50                 new UserInfo{Id=3, Name="Tom", Age=25, City="XiaMen" }51             };52 53             redis.Set<byte[]>("userlist", ser.Serialize(userList));54             List<UserInfo> userList1 = ser.Deserialize(redis.Get<byte[]>("userlist")) as List<UserInfo>;55             userList1.ForEach(i =>56             {57                 Console.WriteLine("id={0},name={1},age={2},city={3}", i.Id, i.Name, i.Age, i.City);58             });59 60             61             Console.Read();62         }63 64         [Serializable]65         class UserInfo66         {67             public int Id { get; set; }68             public string Name { get; set; }69             public int Age { get; set; }70 71             public string City { get; set; }72         }73     }74 }

 

Redis linux環境下查看:

[email protected]:/usr/local/redis/bin# ./redis-cli -a anny
127.0.0.1:6379> keys *
1) "age"
2) "colourList"
3) "int_age"
4) "user1"
5) "userlist"
6) "user_1"
127.0.0.1:6379> type colourList
list
127.0.0.1:6379> lrange colourList 0 -1
1) "red"
2) "pink"
3) "green"
4) "blue"
5) "black"
6) "white"
127.0.0.1:6379> type userlist
string
127.0.0.1:6379> get userlist
"\x00\x01\x00\x00\x00\xff\xff\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x02\x00\x00\[email protected], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\x04\x01\x00\x00\x00\x81\x01System.Collections.Generic.List`1[[RedisDemo.Program+UserInfo, RedisDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\x03\x00\x00\x00\x06_items\x05_size\b_version\x04\x00\x00\x1cRedisDemo.Program+UserInfo[]\x02\x00\x00\x00\b\b\t\x03\x00\x00\x00\x02\x00\x00\x00\x02\x00\x00\x00\a\x03\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x04\x1aRedisDemo.Program+UserInfo\x02\x00\x00\x00\t\x04\x00\x00\x00\t\x05\x00\x00\x00\r\x02\x05\x04\x00\x00\x00\x1aRedisDemo.Program+UserInfo\x04\x00\x00\x00\x13<Id>k__BackingField\x15<Name>k__BackingField\x14<Age>k__BackingField\x15<City>k__BackingField\x00\x01\x00\x01\b\b\x02\x00\x00\x00\x02\x00\x00\x00\x06\x06\x00\x00\x00\x04Jack\x1b\x00\x00\x00\x06\a\x00\x00\x00\abeijing\x01\x05\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x06\b\x00\x00\x00\x03Tom\x19\x00\x00\x00\x06\t\x00\x00\x00\x06XiaMen\x0b"

127.0.0.1:6379> type user_1
string
127.0.0.1:6379> get user_1
"{\"Id\":1,\"Name\":\"Mark\",\"Age\":32,\"City\":\"ShangHai\"}"

Redis在.net中的應用學習

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.