C#操作REDIS例子

C#操作REDIS例子

 

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

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

 

c#使用:

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

 

  1.     static void Main(string[] args)
  2.            {
  3.                var Redis = new RedisClient(“127.0.0.1″, 6379);//redis服务IP和端口
  4.                #region =insert=
  5.                //var storeMembers = new List<string> { ”jj”, ”lihui”, ”cc” };
  6.                //storeMembers.ForEach(x => Redis.AddItemToList(“additemtolist”, x));
  7.                //注:也可直接使用AddRangeToList方法将一组数据装入如:
  8.                //Redis.AddRangeToList(“additemtolist”, storeMembers);
  9.                #endregion
  10.                #region =get=
  11.                //var members = Redis.GetAllItemsFromList(“additemtolist”);
  12.                //members.ForEach(s => Console.WriteLine(“additemtolist :” + s));
  13.                //Console.WriteLine(Redis.GetItemFromList(“additemtolist”, 2));
  14.                #endregion
  15.                #region =delete=
  16.                //var list = Redis.Lists["additemtolist"];
  17.                //list.Clear();//清空
  18.                //list.Remove(“two”);//移除指定键值
  19.                //list.RemoveAt(2);//移除指定索引位置数据
  20.                #endregion
  21.                #region =object=
  22.                //Redis.Set(“userinfo”, new UserInfo() { UserName = ”李四”, Age = 45 });
  23.                //UserInfo userinfo = Redis.Get<UserInfo>(“userinfo”);
  24.                //Console.WriteLine(userinfo.UserName);
  25.                //Redis.Set<int>(“my_age”, 12);//或Redis.Set(“my_age”, 12);
  26.                //Console.WriteLine(Redis.Get<int>(“my_age”));
  27.                #endregion
  28.                var ser = new ObjectSerializer();    //位于namespace ServiceStack.Redis.Support;
  29.                #region =序列化=
  30.                //bool result = Redis.Set<byte[]>(“userinfo”, ser.Serialize(new UserInfo() { UserName = ”张三”, Age = 12 }));
  31.                //UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>(“userinfo”)) as UserInfo;
  32.                //Console.WriteLine(userinfo.UserName);
  33.                #endregion
  34.                //也支持列表
  35.                //Redis.Set<byte[]>(“userinfolist_serialize”, ser.Serialize(userinfoList));
  36.                #region =负载均衡=
  37.                PooledRedisClientManager prcm = CreateManager(new string[] { “10.0.4.210:6379″ }, new string[] { “10.0.4.210:6379″ });
  38.                List<UserInfo> userinfoList = new List<UserInfo>();
  39.                userinfoList.Add(new UserInfo() { UserName = “pool_daizhj”, Age = 1 });
  40.                userinfoList.Add(new UserInfo() { UserName = “pool_daizhj1″, Age = 2 });
  41.                // 从池中获取一个链接:
  42.                using (IRedisClient Redis2 = prcm.GetClient())
  43.                {
  44.                    Redis2.Set(“userinfolist”, userinfoList);
  45.                    List<UserInfo> userList = Redis2.Get<List<UserInfo>>(“userinfolist”);
  46.                }
  47.                #endregion
  48.                Console.ReadLine();
  49.            }
  50.            public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
  51.            {
  52.                //支持读写分离,均衡负载
  53.                return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
  54.                {
  55.                    MaxWritePoolSize = 5,//“写”链接池链接数
  56.                    MaxReadPoolSize = 5,//“写”链接池链接数
  57.                    AutoStart = true,
  58.                });
  59.            }
  60.  
  61.  static void Main(string[] args)
  62.         {
  63.             var Redis = new RedisClient(“127.0.0.1″, 6379);//redis服务IP和端口
  64.  
  65.             #region =insert=
  66.  
  67.             //var storeMembers = new List<string> { ”jj”, ”lihui”, ”cc” };
  68.             //storeMembers.ForEach(x => Redis.AddItemToList(“additemtolist”, x));
  69.             //注:也可直接使用AddRangeToList方法将一组数据装入如:
  70.  
  71.             //Redis.AddRangeToList(“additemtolist”, storeMembers);
  72.             #endregion
  73.  
  74.             #region =get=
  75.  
  76.             //var members = Redis.GetAllItemsFromList(“additemtolist”);
  77.             //members.ForEach(s => Console.WriteLine(“additemtolist :” + s));
  78.             //Console.WriteLine(Redis.GetItemFromList(“additemtolist”, 2));
  79.             #endregion
  80.  
  81.             #region =delete=
  82.  
  83.             //var list = Redis.Lists["additemtolist"];
  84.             //list.Clear();//清空
  85.             //list.Remove(“two”);//移除指定键值
  86.             //list.RemoveAt(2);//移除指定索引位置数据
  87.             #endregion
  88.             #region =object=
  89.  
  90.             //Redis.Set(“userinfo”, new UserInfo() { UserName = ”李四”, Age = 45 });
  91.             //UserInfo userinfo = Redis.Get<UserInfo>(“userinfo”);
  92.             //Console.WriteLine(userinfo.UserName);
  93.  
  94.             //Redis.Set<int>(“my_age”, 12);//或Redis.Set(“my_age”, 12);
  95.             //Console.WriteLine(Redis.Get<int>(“my_age”));
  96.             #endregion
  97.  
  98.             var ser = new ObjectSerializer();    //位于namespace ServiceStack.Redis.Support;
  99.             #region =序列化=
  100.             //bool result = Redis.Set<byte[]>(“userinfo”, ser.Serialize(new UserInfo() { UserName = ”张三”, Age = 12 }));
  101.             //UserInfo userinfo = ser.Deserialize(Redis.Get<byte[]>(“userinfo”)) as UserInfo;
  102.             //Console.WriteLine(userinfo.UserName);
  103.             #endregion
  104.  
  105.             //也支持列表
  106.             //Redis.Set<byte[]>(“userinfolist_serialize”, ser.Serialize(userinfoList));
  107.  
  108.             #region =负载均衡=
  109.  
  110.             PooledRedisClientManager prcm = CreateManager(new string[] { “10.0.4.210:6379″ }, new string[] { “10.0.4.210:6379″ });
  111.             List<UserInfo> userinfoList = new List<UserInfo>();
  112.             userinfoList.Add(new UserInfo() { UserName = “pool_daizhj”, Age = 1 });
  113.             userinfoList.Add(new UserInfo() { UserName = “pool_daizhj1″, Age = 2 });
  114.  
  115.             // 从池中获取一个链接:
  116.  
  117.             using (IRedisClient Redis2 = prcm.GetClient())
  118.             {
  119.                 Redis2.Set(“userinfolist”, userinfoList);
  120.                 List<UserInfo> userList = Redis2.Get<List<UserInfo>>(“userinfolist”);
  121.             }
  122.  
  123.             #endregion
  124.             Console.ReadLine();
  125.         }
  126.         public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
  127.         {
  128.             //支持读写分离,均衡负载
  129.             return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
  130.             {
  131.                 MaxWritePoolSize = 5,//“写”链接池链接数
  132.                 MaxReadPoolSize = 5,//“写”链接池链接数
  133.                 AutoStart = true,
  134.             });
  135.         }

从网上收集而来,仅作自己资料记录.

posted @ 2013-06-07 12:30  羽化成蝶  阅读(1393)  评论(2编辑  收藏  举报