Redis Client for .net组件
2013-12-27 11:41
309 查看
以上分别配置读/写服务地址,默认开启的连接数是9个,访问数据库是0;根据实际应用的需要读/写都可以配置多个redis服务信息.
String Get/Set
Json Get/Set
Protobuf Get/Set
List
MapSet
NorthWind Sample
Source Project
Redis .NET开源组件Beetle.Redis
Beetle.Redis是一款开源的Redis Client for .net组件,它提供非常简便的操作方式可以让开发人员轻松地访问Redis,同时提供json和protobuf的数据格式支持.基于连接池的默认访问方式可以让开发人员简洁高效地访问redis同时,而不必关心线程和连接同步等一系列复杂的事情.配置
组件在使用前要进行配置,主要用于描述访问Redis的信息,分别是读写服务表列.<configSections> <section name="redisClientSection" type="Beetle.Redis.RedisClientSection, Beetle.Redis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </configSections> <redisClientSection dB="0" xmlns="urn:Beetle.Redis"> <writes> <add host="192.168.0.105" connections="9"/> </writes> <reads> <add host="192.168.0.105" connections="9"/> </reads> </redisClientSection>
以上分别配置读/写服务地址,默认开启的连接数是9个,访问数据库是0;根据实际应用的需要读/写都可以配置多个redis服务信息.
使用
组件的使用非常简单,在使用前并不需要象其他redis client组件一样定义连接信息,组件在缺省的情况下会自动使用 redisClientSection的配置环境去操作相应的Redis服务.String Get/Set
StringKey key = "HENRY"; string Remark = "henryfan gz cn 18 henryfan@msn.com 28304340"; key.Set(Remark); Assert.AreEqual(Remark, key.Get<string>());
Json Get/Set
JsonKey rk = "henry_json"; UserBase ub = new UserBase(); ub.Name = "henryfan"; ub.City = "gz"; ub.Counrty = "cn"; ub.Age = 10; rk.Set(ub); Assert.AreEqual(ub.Name, rk.Get<UserBase>().Name);
Protobuf Get/Set
ProtobufKey rk = "henry_protobuf"; UserBase ub = new UserBase(); ub.Name = "henryfan"; ub.City = "gz"; ub.Counrty = "cn"; ub.Age = 10; rk.Set(ub); Assert.AreEqual(ub.Name, rk.Get<UserBase>().Name);
List
[TestMethod] public void LST_POP_PUSH() { ProtobufList<UserBase> lst = "USERS"; lst.Push(new UserBase { Name = "henry", Age = 18, City = "gz", Counrty = "cn" }); Assert.AreEqual("henry", lst.Pop().Name); } [TestMethod] public void LST_REMOVE_ADD() { ProtobufList<UserBase> lst = "USERS"; lst.Add(new UserBase { Name = "henry", Age = 18, City = "gz", Counrty = "cn" }); lst.Add(new UserBase { Name = "bbq", Age = 18, City = "gz", Counrty = "cn" }); Assert.AreEqual("bbq", lst.Remove().Name); } [TestMethod] public void LST_Length() { ProtobufList<UserBase> lst = "USERS"; lst.Clear(); lst.Add(new UserBase { Name = "henry", Age = 18, City = "gz", Counrty = "cn" }); lst.Add(new UserBase { Name = "bbq", Age = 18, City = "gz", Counrty = "cn" }); Assert.AreEqual(lst.Count(), 2); } [TestMethod] public void LST_Region() { ProtobufList<UserBase> lst ="USERS"; lst.Clear(); for (int i = 0; i < 10; i++) { lst.Add(new UserBase { Name = "henry" + i, Age = 18, City = "gz", Counrty = "cn" }); } IList<UserBase> items = lst.Range(); Assert.AreEqual(items[0].Name, "henry0"); Assert.AreEqual(items[9].Name, "henry9"); items = lst.Range(5, 7); Assert.AreEqual(items[0].Name, "henry5"); Assert.AreEqual(items[2].Name, "henry7"); }
MapSet
[TestMethod] public void MapSet() { JsonMapSet map = "HENRY_INFO"; UserBase ub = new UserBase(); ub.Name = "henryfan"; ub.City = "gz"; ub.Counrty = "cn"; ub.Age = 10; Contact contact = new Contact(); contact.EMail = "hernyfan@msn.com"; contact.QQ = "28304340"; contact.Phone = "13660223497"; map.Set(ub, contact); IList<object> data = map.Get<UserBase, Contact>(); Assert.AreEqual(ub.Name, ((UserBase)data[0]).Name); Assert.AreEqual(contact.Phone, ((Contact)data[1]).Phone); } [TestMethod] public void MapSetdRemove() { JsonMapSet map = "HENRY_INFO"; UserBase ub = new UserBase(); ub.Name = "henryfan"; ub.City = "gz"; ub.Counrty = "cn"; ub.Age = 10; Contact contact = new Contact(); contact.EMail = "hernyfan@msn.com"; contact.QQ = "28304340"; contact.Phone = "13660223497"; map.Set(ub, contact); map.Remove<Contact>(); contact = map.Get<Contact>(); Assert.AreEqual(null, contact); } [TestMethod] public void MapSetClear() { JsonMapSet map = "HENRY_INFO"; UserBase ub = new UserBase(); ub.Name = "henryfan"; ub.City = "gz"; ub.Counrty = "cn"; ub.Age = 10; Contact contact = new Contact(); contact.EMail = "hernyfan@msn.com"; contact.QQ = "28304340"; contact.Phone = "13660223497"; map.Set(ub, contact); map.Clear(); IList<object> data = map.Get<UserBase, Contact>(); Assert.AreEqual(null, data[0]); Assert.AreEqual(null, data[1]); }
性能
Sample
下载
Beetle.Redis 0.6NorthWind Sample
Source Project
相关文章推荐
- Redis For .NET开源组件Beetle.Redis
- NEST.net Client For Elasticsearch简单应用
- 关于FirebirdClient 2.1 for .net 2.0 provider对TransactionScope的支持问题
- FastDFS client for .net
- 专业性的PDF文档创建组件Spire.PDF for .NET控件
- MenuPilot - 非常棒的开源任务菜单组件 For ASP.Net 2.0
- Redis for .NET 系列之实现分页需求
- NuGet安装,通过NuGet安装Couchbase Client Library for .NET
- Python Client For Redis On Windows
- Word组件Spire.Doc for .NET V6.0.31发布 | 支持免费下载
- NEST.net Client For Elasticsearch简单应用
- Word组件 Spire.Doc for .NET V6.0.3发布 | 修复多个重大bug
- PDF文档创建组件Spire.PDF for .NET
- 【转载】COM 组件设计与应用(十)——IDispatch 接口 for VC.NET
- Practical Multithreading for Client Apps (.net客户端程序使用多线程的实践)
- NuGet安装,通过NuGet安装Couchbase Client Library for .NET
- 音频视频组件Audio DJ Studio for .NET更新至v10.0.0.0丨附下载
- 专业性的PDF文档创建组件 Spire.PDF for .NET
- An FTP client library for .NET 2.0
- ssis 的For each container组件中,若有 Execute SQL TASK Editor且其含参, 后者的Connection Type应设为 ADO.NET