{"id":185,"date":"2011-07-25T15:49:51","date_gmt":"2011-07-25T15:49:51","guid":{"rendered":"eecca5b6365d9607ee5a9d336962c534"},"modified":"2011-07-25T15:49:51","modified_gmt":"2011-07-25T15:49:51","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/185","title":{"rendered":".NET \u5ba2\u6237\u7aef\u8fde\u63a5Redis \u4ecb\u7ecd"},"content":{"rendered":"<div id=\"blog_text\" class=\"cnt\">\n<p>C#\u5199\u7684\u5ba2\u6237\u7aef\u7c7b\u578b\u4e3b\u8981\u6709\uff1a<\/p>\n<table style=\"width:852px;height:102px;\">\n<tbody>\n<tr>\n<td>ServiceStack.Redis <span>\u2605&nbsp; <a href=\"https:\/\/github.com\/ServiceStack\/ServiceStack.Redis\">https:\/\/github.com\/ServiceStack\/ServiceStack.Redis<\/a><\/span><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Booksleeve <span>\u2605&nbsp;&nbsp; <a href=\"http:\/\/code.google.com\/p\/booksleeve\/\">http:\/\/code.google.com\/p\/booksleeve\/<\/a><\/span><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Sider&nbsp; <a href=\"http:\/\/nuget.org\/List\/Packages\/Sider\">http:\/\/nuget.org\/List\/Packages\/Sider<\/a><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>TeamDev Redis Client&nbsp; <a href=\"http:\/\/redis.codeplex.com\/\">http:\/\/redis.codeplex.com\/<\/a><\/td>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>redis-sharp&nbsp;&nbsp; <a href=\"https:\/\/github.com\/migueldeicaza\/redis-sharp\">https:\/\/github.com\/migueldeicaza\/redis-sharp<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u52a0\u661f\u53f7\u7684\u662f\u4f7f\u7528\u7387\u9ad8\u7684\u9879\u76ee\u3002\u53c2\u8003\uff1a<a href=\"http:\/\/redis.io\/clients\">http:\/\/redis.io\/clients<\/a><\/p>\n<p>\u4ee5<strong><span style=\"color:#ff0000;\">ServiceStack.Redis<\/span><\/strong>\u4e3a\u4f8b<\/p>\n<p>\u5f15\u5165\u5230\u9879\u76ee\u4e2d\uff0c\u5e76\u6dfb\u52a0\u5982\u4e0b\u540d\u7a7a\u95f4\u5f15\u7528(\u4ec5\u9650\u672c\u6587):\n<\/p>\n<div>\n<div><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> ServiceStack.Common.Extensions;<br \/>\n<\/span><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> ServiceStack.Redis;<br \/>\n<\/span><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> ServiceStack.Redis.Generic;<br \/>\n<\/span><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> ServiceStack.Text;<br \/>\n<\/span><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> ServiceStack.Redis.Support;<\/span><\/div>\n<\/div>\n<p>\n\u6ce8\uff1aServiceStackRedis\u5c01\u88c5\u4e86\u5927\u91cf\u65b9\u6cd5\u548c\u5bf9\u8c61\uff0c\u8fd9\u91cc\u53ea\u6458\u6709\u4ee3\u8868\u6027\u7684\u5185\u5bb9\u4ecb\u7ecd\uff0c\u66f4\u591a\u5185\u5bb9\u53c2\u89c1\u5176\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u58f0\u660e\u4e00\u4e2a\u5ba2\u6237\u7aef\u5bf9\u8c61\uff1a<br \/>\nprotected RedisClient Redis = new RedisClient(\"127.0.0.1\", 6379);\/\/redis\u670d\u52a1IP\u548c\u7aef\u53e3<\/p>\n<p>\n\u4e00 .\u57fa\u672cKEY\/VALUE\u952e\u503c\u5bf9\u64cd\u4f5c\uff1a<br \/>\n1. <span style=\"color:red;\">\u6dfb\u52a0\/\u83b7\u53d6:<\/span>\n<\/p>\n<div>\n<div><span style=\"color:#000000;\">List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\"> storeMembers <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">();<br \/>\nstoreMembers.ForEach(x <\/span><span style=\"color:#000000;\">=&gt;<\/span><span style=\"color:#000000;\"> Redis.AddItemToList(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">additemtolist<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, x));<\/span><\/div>\n<\/div>\n<p><span style=\"font-size:10pt;\">\u6ce8\uff1a\u4e5f\u53ef\u76f4\u63a5\u4f7f\u7528AddRangeToList\u65b9\u6cd5\u5c06\u4e00\u7ec4\u6570\u636e\u88c5\u5165\u5982\uff1a<\/span><\/p>\n<p>Redis.AddRangeToList(\"addarrangetolist\", storeMembers);\n<\/p>\n<\/p>\n<p>2<span style=\"color:red;\">. \u83b7\u53d6\u6570\u636e<\/span>\n<\/p>\n<div>\n<div><span style=\"color:#000000;\">var members <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.GetAllItemsFromList(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">additemtolist<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">);<br \/>\nmembers.ForEach(s <\/span><span style=\"color:#000000;\">=&gt;<\/span><span style=\"color:#000000;\"> Response.Write(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">&lt;br\/&gt;additemtolist :<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">+<\/span><span style=\"color:#000000;\"> s));<\/span><\/div>\n<\/div>\n<p>\n3. <span style=\"color:red;\">\u83b7\u53d6\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u6570\u636e<\/span>\n<\/p>\n<div>\n<div><span style=\"color:#000000;\">var item <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.GetItemFromList(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">addarrangetolist<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#800080;\">2<\/span><span style=\"color:#000000;\">);<\/span><\/div>\n<\/div>\n<p>\n4. <span style=\"color:red;\">\u79fb\u9664\uff1a<\/span>\n<\/p>\n<div>\n<div><span style=\"color:#000000;\">var list <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.Lists[<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">addarrangetolist<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">];<br \/>\nlist.Clear();<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u6e05\u7a7a<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#000000;\">list.Remove(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">two<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">);<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u79fb\u9664\u6307\u5b9a\u952e\u503c<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#000000;\">list.RemoveAt(<\/span><span style=\"color:#800080;\">2<\/span><span style=\"color:#000000;\">);<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u79fb\u9664\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u6570\u636e<\/span><\/div>\n<\/div>\n<p>\n<\/p>\n<p>\u4e8c.\u5b58\u50a8\u5bf9\u8c61\uff1a\n<\/p>\n<div>\n<div><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#0000ff;\">class<\/span><span style=\"color:#000000;\"> UserInfo<br \/>\n{<br \/>\n<\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#0000ff;\">long<\/span><span style=\"color:#000000;\"> Id { <\/span><span style=\"color:#0000ff;\">set<\/span><span style=\"color:#000000;\">; <\/span><span style=\"color:#0000ff;\">get<\/span><span style=\"color:#000000;\">; }<br \/>\n<\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> UserName { <\/span><span style=\"color:#0000ff;\">get<\/span><span style=\"color:#000000;\">; <\/span><span style=\"color:#0000ff;\">set<\/span><span style=\"color:#000000;\">; }<br \/>\n<\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#0000ff;\">int<\/span><span style=\"color:#000000;\"> Age { <\/span><span style=\"color:#0000ff;\">get<\/span><span style=\"color:#000000;\">; <\/span><span style=\"color:#0000ff;\">set<\/span><span style=\"color:#000000;\">; }<br \/>\n}<\/span><\/div>\n<\/div>\n<p>\n1.<span style=\"color:red;\">\u901a\u5e38\u65b9\u5f0f\uff08\u5e95\u5c42\u4f7f\u7528json\u5e8f\u5217\u5316\uff09\uff1a<\/span>\n<\/p>\n<div>\n<div><span style=\"color:#000000;\">Redis.Set<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfo<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> UserInfo() { UserName <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">\u674e\u56db<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, Age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">45<\/span><span style=\"color:#000000;\"> });<br \/>\nUserInfo userinfo <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.Get<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfo<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">);<\/span><\/div>\n<\/div>\n<p>\n\u6ce8\uff1a\u5f53\u7136\u4e0a\u9762\u65b9\u5f0f\u4e5f\u9002\u5408\u4e8e\u57fa\u672c\u7c7b\u578b\uff0c\u5982\uff1a\n<\/p>\n<div>\n<div><span style=\"color:#000000;\">Redis.Set<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">int<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">my_age<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#800080;\">12<\/span><span style=\"color:#000000;\">);<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u6216Redis.Set(\"my_age\", 12);<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#0000ff;\">int<\/span><span style=\"color:#000000;\"> age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.Get<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">int<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">my_age<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">);<\/span><\/div>\n<\/div>\n<p>2.<span style=\"color:red;\">object\u5e8f\u5217\u5316\u65b9\u5f0f\u5b58\u50a8\uff1a<\/span>\n<\/p>\n<div><img decoding=\"async\" class=\"code_img_opened\" src=\"http:\/\/images.cnblogs.com\/OutliningIndicators\/ExpandedBlockStart.gif\" \/> <\/p>\n<div>\n<div><span style=\"color:#000000;\">var ser <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> ObjectSerializer(); <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u4f4d\u4e8enamespace ServiceStack.Redis.Support;<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#0000ff;\">bool<\/span><span style=\"color:#000000;\"> result <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.Set<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">byte<\/span><span style=\"color:#000000;\">[]<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfo<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, ser.Serialize(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> UserInfo() { UserName <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">\u5f20\u4e09<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, Age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">12<\/span><span style=\"color:#000000;\"> }));<br \/>\nUserInfo userinfo <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> ser.Deserialize(Redis.Get<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">byte<\/span><span style=\"color:#000000;\">[]<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfo<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">)) <\/span><span style=\"color:#0000ff;\">as<\/span><span style=\"color:#000000;\"> UserInfo;<br \/>\n<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u4e5f\u652f\u6301\u5217\u8868<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#000000;\">Redis.Set<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">byte<\/span><span style=\"color:#000000;\">[]<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfolist_serialize<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, ser.Serialize(userinfoList));<br \/>\nList<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\"> userList <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> ser.Deserialize(Redis.Get<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#0000ff;\">byte<\/span><span style=\"color:#000000;\">[]<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfolist_serialize<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">)) <\/span><span style=\"color:#0000ff;\">as<\/span><span style=\"color:#000000;\"> List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">;<\/span><\/div>\n<\/div>\n<\/div>\n<p>\n\u9700\u8981\u8bf4\u660e\u7684\u662f\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u53d1\u73b0JSON\u5e8f\u5217\u5316\u7684\u6548\u7387\u8981\u6bd4<span style=\"color:#000000;\">object\u5e8f\u5217\u5316<\/span>\u9ad8\u4e00\u4e9b\u3002<\/p>\n<p>\u4e09.\u5b58\u50a8\u8868\u683c\u5bf9\u8c61\uff0c\u6bd4\u5982\uff1a<\/p>\n<div><img decoding=\"async\" class=\"code_img_opened\" src=\"http:\/\/images.cnblogs.com\/OutliningIndicators\/ExpandedBlockStart.gif\" \/> <\/p>\n<div>\n<div><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> (var redisUsers <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.GetTypedClient<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">())<br \/>\n{<br \/>\nredisUsers.Store(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> UserInfo { Id <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> redisUsers.GetNextSequence(), UserName <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">daizhj<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, Age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">12<\/span><span style=\"color:#000000;\"> });<br \/>\nredisUsers.Store(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> UserInfo { Id <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> redisUsers.GetNextSequence(), UserName <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">daizhenjun<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, Age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">13<\/span><span style=\"color:#000000;\"> });<\/p>\n<p>var allUsers <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> redisUsers.GetAll();<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u5c31\u50cf\u64cd\u4f5cado\u5bf9\u8c61\u4e00\u6837\uff0c\u53ef\u4ee5\u8fdb\u884cCRUD\u7b49\u64cd\u4f5c<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#000000;\">allUsers.ForEach(s <\/span><span style=\"color:#000000;\">=&gt;<\/span><span style=\"color:#000000;\"> Response.Write(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">&lt;br\/&gt;user :<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">+<\/span><span style=\"color:#000000;\"> s.UserName <\/span><span style=\"color:#000000;\">+<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\"> age:<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">+<\/span><span style=\"color:#000000;\"> s.Age));<br \/>\n}<\/span><\/div>\n<\/div>\n<\/div>\n<p>\u56db.\u4f7f\u7528\u5ba2\u6237\u7aef\u94fe\u63a5\u6c60\u6a21\u5f0f\u63d0\u5347\u94fe\u63a5\u901f\u5ea6\uff1a<\/p>\n<div><img decoding=\"async\" class=\"code_img_opened\" src=\"http:\/\/images.cnblogs.com\/OutliningIndicators\/ExpandedBlockStart.gif\" \/> <\/p>\n<div>\n<div><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#0000ff;\">static<\/span><span style=\"color:#000000;\"> PooledRedisClientManager CreateManager(<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\">[] readWriteHosts, <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\">[] readOnlyHosts)<br \/>\n{<br \/>\n<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u652f\u6301\u8bfb\u5199\u5206\u79bb\uff0c\u5747\u8861\u8d1f\u8f7d<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> PooledRedisClientManager(readWriteHosts, readOnlyHosts, <\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> RedisClientManagerConfig<br \/>\n{<br \/>\nMaxWritePoolSize <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">5<\/span><span style=\"color:#000000;\">,<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u201c\u5199\u201d\u94fe\u63a5\u6c60\u94fe\u63a5\u6570<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#000000;\">MaxReadPoolSize <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">5<\/span><span style=\"color:#000000;\">,<\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">\u201c\u5199\u201d\u94fe\u63a5\u6c60\u94fe\u63a5\u6570<\/span><span style=\"color:#008000;\"><br \/>\n<\/span><span style=\"color:#000000;\">AutoStart <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#0000ff;\">true<\/span><span style=\"color:#000000;\">,<br \/>\n}); <br \/>\n}<\/span><\/div>\n<\/div>\n<\/div>\n<p>\n\u58f0\u660e\u94fe\u63a5\u6c60\u5bf9\u8c61\uff08\u8fd9\u91cc\u53ea\u4f7f\u7528\u4e00\u4e2aredis\u670d\u52a1\u7aef\uff09\uff1a\n<\/p>\n<div><img decoding=\"async\" class=\"code_img_opened\" src=\"http:\/\/images.cnblogs.com\/OutliningIndicators\/ExpandedBlockStart.gif\" \/> <\/p>\n<div>\n<div><span style=\"color:#000000;\">PooledRedisClientManager prcm <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> CreateManager(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\">[] { <\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">127.0.0.1:6379<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\"> }, <\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\">[] { <\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">127.0.0.1:6379<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\"> });<\/p>\n<p>List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\"> userinfoList <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">();<br \/>\nuserinfoList.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> UserInfo() { UserName <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">pool_daizhj<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, Age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">1<\/span><span style=\"color:#000000;\"> });<br \/>\nuserinfoList.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> UserInfo() { UserName <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">pool_daizhj1<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, Age <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#800080;\">2<\/span><span style=\"color:#000000;\"> });<\/span><\/div>\n<\/div>\n<\/div>\n<p>\n\u4ece\u6c60\u4e2d\u83b7\u53d6\u4e00\u4e2a\u94fe\u63a5\uff1a\n<\/p>\n<div>\n<div><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> (IRedisClient Redis <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> prcm.GetClient())<br \/>\n{ <br \/>\nRedis.Set(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfolist<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">, userinfoList);<br \/>\nList<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\"> userList <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> Redis.Get<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">UserInfo<\/span><span style=\"color:#000000;\">&gt;&gt;<\/span><span style=\"color:#000000;\">(<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#800000;\">userinfolist<\/span><span style=\"color:#800000;\">\"<\/span><span style=\"color:#000000;\">);<br \/>\n}<\/span><\/div>\n<\/div>\n<p>\n<\/p>\n<p>\u6ce8\uff1a<br \/>\n1.\u524d\u4e09\u79cd\u65b9\u5f0f\u6211\u5728\u672c\u5730\u6d4b\u8bd5\u53d1\u73b0\u5b58\u53d6\u6548\u7387\u4ece\u9ad8\u5230\u5e95\uff0c\u5177\u4f53\u539f\u56e0\u8fd8\u5f85\u5206\u6790\u3002 <\/p>\n<p>2.\u5982\u53ea\u60f3\u4f7f\u7528\u957f\u94fe\u63a5\u800c\u4e0d\u662f\u94fe\u63a5\u6c60\u7684\u8bdd\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e0b\u9762\u5bf9\u8c61\u7528static\u65b9\u5f0f\u58f0\u660e\u5373\u53ef\uff1a <br \/>\nprotected <span style=\"color:#ff0000;\">static <\/span>RedisClient Redis = new RedisClient(\"127.0.0.1\", 6379); <\/p>\n<p>\u8fd9\u6837\u5728redis\u670d\u52a1\u7aef\u663e\u793a\u53ea\u6709\u4e00\u4e2a\u5ba2\u6237\u94fe\u63a5<\/p>\n<p>3.\u4e0ememcached\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u53d1\u73b0\uff0c\u5728\u5b58\u50a8\u65f6\u4e24\u8005\u6548\u7387\u63a5\u8fd1\uff08\u4f7f\u7528\u672c\u6587\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff09\uff0c\u5728\u53d6\u6570\u636e\u65f6memcached\u901f\u5ea6\u6bd4redis\u8981\u5feb\u4e00\u4e9b\uff08\u6beb\u79d2\u7ea7\u5dee\u5f02\uff09\uff0c\u8fd9\u4e00\u70b9\u5e76\u4e0d\u50cf\u7f51\u4e0a\u4e00\u4e9b\u6587\u7ae0\u6240\u8bf4\u7684\u90a3\u6837\uff0c\u770b\u6765\u5728\u5b9e\u9645\u5f00\u53d1\u548c\u751f\u4ea7\u73af\u5883\u4e0b\u8fd8\u8981\u4ee5\u4f7f\u7528\u80cc\u666f\u53ca\u7ed3\u679c\u4e3a\u51c6\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<div id=\"blog_text\" class=\"cnt\">\n<p>C#\u5199\u7684\u5ba2\u6237\u7aef\u7c7b\u578b\u4e3b\u8981\u6709\uff1a<\/p>\n<table style=\"width:852px;height:102px;\">\n<tbody>\n<tr>\n<td>ServiceStack.Redis <span>\u2605&nbsp; <a href=\"https:\/\/github.com\/ServiceStack\/ServiceStack.Redis\">https:\/\/github.com\/ServiceStack\/ServiceStack.Redis<\/a><\/span><\/td>\n<\/tr>\n<tr>\n<td>Booksleeve <span>\u2605&nbsp;&nbsp; <a href=\"http:\/\/code.google.com\/p\/booksleeve\/\">http:\/\/code.google.com\/p\/booksleeve\/<\/a><\/span><\/td>\n<\/tr>\n<tr>\n<td>Sider&nbsp; <a href=\"http:\/\/nuget.org\/List\/Packages\/Sider\">http:\/\/nuget.org\/List\/Packages\/Sider<\/a><\/td>\n<\/tr>\n<tr>\n<td>TeamDev Redis Client&nbsp; <a href=\"http:\/\/redis.codeplex.com\/\">http:\/\/redis.codeplex.com\/<\/a><\/td>\n<\/tr>\n<tr>\n<td>redis-sharp&nbsp;&nbsp; <a href=\"https:\/\/github.com\/migueldeicaza\/redis-sharp\">https:\/\/github.com\/migue...<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/185\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[282],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-cache"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}