设为首页 收藏本站
查看: 1244|回复: 0

[经验分享] .NET 客户端连接Redis 介绍

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-12 15:03:31 | 显示全部楼层 |阅读模式
ServiceStack.Redis ★  
https://github.com/ServiceStack/ServiceStack.Redis

Booksleeve ★   
http://code.google.com/p/booksleeve/

Sider  http://nuget.org/List/Packages/Sider

TeamDev Redis Client  http://redis.codeplex.com/

redis-sharp   
https://github.com/migueldeicaza/redis-sharp  
  加星号的是使用率高的项目。参考:http://redis.io/clients
  以ServiceStack.Redis为例
  引入到项目中,并添加如下名空间引用(仅限本文):
  

using ServiceStack.Common.Extensions;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using ServiceStack.Text;
using ServiceStack.Redis.Support;
  

注:ServiceStackRedis封装了大量方法和对象,这里只摘有代表性的内容介绍,更多内容参见其官方文档。



声明一个客户端对象:

protected RedisClient Redis = new RedisClient("127.0.0.1", 6379);//redis服务IP和端口





一 .基本KEY/VALUE键值对操作:

1. 添加/获取:



List<string> storeMembers
=new List<string>();

storeMembers.ForEach(x
=> Redis.AddItemToList(&quot;additemtolist&quot;,
x));
  注:也可直接使用AddRangeToList方法将一组数据装入如:
  Redis.AddRangeToList(&quot;addarrangetolist&quot;, storeMembers);


  
  2. 获取数据



var members = Redis.GetAllItemsFromList(&quot;additemtolist&quot;);

members.ForEach(s
=> Response.Write(&quot;<br/>additemtolist :&quot;&#43;
s));
  

3. 获取指定索引位置数据



var item = Redis.GetItemFromList(&quot;addarrangetolist&quot;,
2);  

4. 移除:



var list = Redis.Lists[&quot;addarrangetolist&quot;];

list.Clear();
//清空

list.Remove(&quot;two&quot;);//移除指定键&#20540;

list.RemoveAt(2);//移除指定索引位置数据  


  二.存储对象:



publicclass UserInfo

{

publiclong Id {
set; get; }

publicstring UserName {
get; set; }

publicint Age {
get; set; }

}
  

1.通常方式(底层使用json序列化):



Redis.Set<UserInfo>(&quot;userinfo&quot;,
new UserInfo() { UserName
=&quot;李四&quot;, Age
=45 });

UserInfo userinfo
= Redis.Get<UserInfo>(&quot;userinfo&quot;);  

注:当然上面方式也适合于基本类型,如:



Redis.Set<int>(&quot;my_age&quot;,
12);//或Redis.Set(&quot;my_age&quot;, 12);

int age
= Redis.Get<int>(&quot;my_age&quot;);  



2.object序列化方式存储:



var ser =new ObjectSerializer();
//位于namespace ServiceStack.Redis.Support;

bool result
= Redis.Set<byte[]>(&quot;userinfo&quot;,
ser.Serialize(
new UserInfo() { UserName
=&quot;张三&quot;, Age
=12 }));

UserInfo userinfo
= ser.Deserialize(Redis.Get<byte[]>(&quot;userinfo&quot;))
as UserInfo;

//也支持列表

Redis.Set<byte[]>(&quot;userinfolist_serialize&quot;,
ser.Serialize(userinfoList));

List
<UserInfo> userList
= ser.Deserialize(Redis.Get<byte[]>(&quot;userinfolist_serialize&quot;))
as List<UserInfo>;  

需要说明的是在测试过程中发现JSON序列化的效率要比object序列化高一些。



三.存储表&#26684;对象,比如:





DSC0000.gif
using (var redisUsers
= Redis.GetTypedClient<UserInfo>())

{

redisUsers.Store(
new UserInfo { Id
= redisUsers.GetNextSequence(), UserName
=&quot;daizhj&quot;, Age
=12 });

redisUsers.Store(
new UserInfo { Id
= redisUsers.GetNextSequence(), UserName
=&quot;daizhenjun&quot;, Age
=13 });



var allUsers
= redisUsers.GetAll();//就像操作ado对象一样,可以进行CRUD等操作

allUsers.ForEach(s => Response.Write(&quot;<br/>user :&quot;&#43;
s.UserName
&#43;&quot; age:&quot;&#43; s.Age));

}
  



四.使用客户端链接池模式提升链接速度:






publicstatic PooledRedisClientManager CreateManager(string[] readWriteHosts,
string[] readOnlyHosts)

{

//支持读写分离,均衡负载

returnnew PooledRedisClientManager(readWriteHosts, readOnlyHosts,
new RedisClientManagerConfig

{

MaxWritePoolSize
=5,//“写”链接池链接数

MaxReadPoolSize =5,//“写”链接池链接数

AutoStart =true,

});

}
  

声明链接池对象(这里只使用一个redis服务端):




PooledRedisClientManager prcm = CreateManager(newstring[]
{
&quot;127.0.0.1:6379&quot; },
newstring[] {
&quot;127.0.0.1:6379&quot; });



List
<UserInfo> userinfoList
=new List<UserInfo>();

userinfoList.Add(
new UserInfo() { UserName
=&quot;pool_daizhj&quot;, Age
=1 });

userinfoList.Add(
new UserInfo() { UserName
=&quot;pool_daizhj1&quot;, Age
=2 });  

从池中获取一个链接:



using (IRedisClient Redis
= prcm.GetClient())

{

Redis.Set(
&quot;userinfolist&quot;, userinfoList);

List
<UserInfo> userList
= Redis.Get<List<UserInfo>>(&quot;userinfolist&quot;);

}
  


  注:

1.前三种方式我在本地测试发现存取效率从高到底,具体原因还待分析。
  2.如只想使用长链接而不是链接池的话,可以直接将下面对象用static方式声明即可:

protected static RedisClient Redis = new RedisClient(&quot;127.0.0.1&quot;, 6379);

  这样在redis服务端显示只有一个客户链接
  3.与memcached测试过程中发现,在存储时两者效率接近(使用本文第一种方式),在取数据时memcached速度比redis要快一些(毫秒级差异),这一点并不像网上一些文章所说的那样,看来在实际开发和生产环境下还要以使用背景及结果为准。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-138417-1-1.html 上篇帖子: Redis Hash操作 下篇帖子: Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表