C#操作REDIS例子

系统 1997 0
  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.          }

 

C#操作REDIS例子


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论