redis在.NET下的使用

windows SEVER包:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload

windows仅用来测试,性能不如在linux下高.

 

c#使用:

引用的ServiceStackRedis:https://github.com/mythz/ServiceStack.Redis

 

 

[csharp] view plaincopy
 
  1. static void Main(string[] args)  
  2.        {  
  3.            var Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口  
  4.  
  5.            #region =insert=  
  6.   
  7.            //var storeMembers = new List<string> { "jj", "lihui", "cc" };  
  8.            //storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));  
  9.            //注:也可直接使用AddRangeToList方法将一组数据装入如:  
  10.   
  11.            //Redis.AddRangeToList("additemtolist", storeMembers);  
  12.            #endregion  
  13.  
  14.            #region =get=  
  15.   
  16.            //var members = Redis.GetAllItemsFromList("additemtolist");  
  17.            //members.ForEach(s => Console.WriteLine("additemtolist :" + s));  
  18.            //Console.WriteLine(Redis.GetItemFromList("additemtolist", 2));  
  19.            #endregion  
  20.  
  21.            #region =delete=  
  22.   
  23.            //var list = Redis.Lists["additemtolist"];  
  24.            //list.Clear();//清空  
  25.            //list.Remove("two");//移除指定键值  
  26.            //list.RemoveAt(2);//移除指定索引位置数据  
  27.            #endregion  
  28.            #region =object=  
  29.   
  30.            //Redis.Set("userinfo", new UserInfo() { UserName = "李四", Age = 45 });  
  31.            //UserInfo userinfo = Redis.Get<UserInfo>("userinfo");  
  32.            //Console.WriteLine(userinfo.UserName);  
  33.   
  34.            //Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);  
  35.            //Console.WriteLine(Redis.Get<int>("my_age"));  
  36.            #endregion  
  37.   
  38.            var ser = new ObjectSerializer();    //位于namespace ServiceStack.Redis.Support;  
  39.            #region =序列化=  
  40.            //bool result = Redis.Set<byte[]>("userinfo", ser.Serialize(new UserInfo() { UserName = "张三", Age = 12 }));  
  41.            //UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>("userinfo")) as UserInfo;  
  42.            //Console.WriteLine(userinfo.UserName);  
  43.            #endregion  
  44.   
  45.            //也支持列表  
  46.            //Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));  
  47.  
  48.            #region =负载均衡=  
  49.   
  50.            PooledRedisClientManager prcm = CreateManager(new string[] { "10.0.4.210:6379" }, new string[] { "10.0.4.210:6379" });  
  51.            List<UserInfo> userinfoList = new List<UserInfo>();  
  52.            userinfoList.Add(new UserInfo() { UserName = "pool_daizhj", Age = 1 });  
  53.            userinfoList.Add(new UserInfo() { UserName = "pool_daizhj1", Age = 2 });  
  54.   
  55.   
  56.            // 从池中获取一个链接:  
  57.   
  58.            using (IRedisClient Redis2 = prcm.GetClient())  
  59.            {  
  60.                Redis2.Set("userinfolist", userinfoList);  
  61.                List<UserInfo> userList = Redis2.Get<List<UserInfo>>("userinfolist");  
  62.            }  
  63.  
  64.            #endregion  
  65.            Console.ReadLine();  
  66.        }  
  67.        public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)  
  68.        {  
  69.            //支持读写分离,均衡负载  
  70.            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig  
  71.            {  
  72.                MaxWritePoolSize = 5,//“写”链接池链接数  
  73.                MaxReadPoolSize = 5,//“写”链接池链接数  
  74.                AutoStart = true,  
  75.            });  
  76.        }  
posted @ 2013-06-23 21:23  潇湘〃细雨  阅读(526)  评论(0编辑  收藏  举报