servicestack操作redis

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   使用   sp   

tatic void Main(string[] args)         {             var Redis = new RedisClient("127.0.0.1", 6379);//redis服務IP和連接埠              #region =insert=               var storeMembers = new List<string> { "jj", "lihui", "cc" };             storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));             //註:也可直接使用AddRangeToList方法將一組資料裝入如:               Redis.AddRangeToList("additemtolist", storeMembers);             #endregion              #region =get=               var members = Redis.GetAllItemsFromList("additemtolist");             members.ForEach(s => Console.WriteLine("additemtolist :" + s));             Console.WriteLine(Redis.GetItemFromList("additemtolist", 2));             #endregion              #region =delete=               var list = Redis.Lists["additemtolist"];             list.Clear();//清空             list.Remove("two");//移除指定索引值             list.RemoveAt(2);//移除指定索引位置資料             #endregion             #region =object=               Redis.Set("userinfo", new UserInfo() { UserName = "李四", Age = 45 });             UserInfo userinfo = Redis.Get<UserInfo>("userinfo");             Console.WriteLine(userinfo.UserName);               Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);             Console.WriteLine(Redis.Get<int>("my_age"));             #endregion               var ser = new ObjectSerializer();    //位於namespace ServiceStack.Redis.Support;             #region =序列化=             bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName = "張三", Age = 12 }));             UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;             Console.WriteLine(userinfo.UserName);             #endregion               //也支援列表             Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));              #region =負載平衡=               PooledRedisClientManager prcm = CreateManager(new string[] { "10.0.4.210:6379" }, new string[] { "10.0.4.210:6379" });             List<UserInfo> userinfoList = new List<UserInfo>();             userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });             userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });                 // 從池中擷取一個連結:               using (IRedisClient Redis2 = prcm.GetClient())             {                 Redis2.Set("userinfolist", userinfoList);                 List<UserInfo> userList = Redis2.Get<List<UserInfo>>("userinfolist");             }              #endregion             Console.ReadLine();         }         public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)         {             //支援讀寫分離,均衡負載             return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig             {                 MaxWritePoolSize = 5,//“寫”連結池連結數                 MaxReadPoolSize = 5,//“寫”連結池連結數                 AutoStart = true,             });         }  

 

servicestack操作redis

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.