Redis IRedisTypedClient
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Redis IRedisTypedClient,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3334字,纯文字阅读大概需要5分钟。
内容图文
IRedisTypedClient
IRedisTypedClient类相当于IRedicClient的强类型版,其方法与属性大多数与IRedisClient类似。
它支持在Redis中使用Linq查询的强大的类,它本身是一个泛型,IRedisClient的泛型方法As获得对象。
其方法原型如下:
IRedisTypedClient<T> As<T>();
1、IEntityStore<T>接口内容
其中IRedisTypedClient这个类实现了这个接口IEntityStore<T>,该接口要求提供的功能如下:
方法 | 说明 |
Delete | 根据实体删除一条记录 |
DeleteAll | 全部删除 |
DeleteById | 根据Id删除一条记录 |
DeleteByIds | 根据输入的多个Id删除多条记录 |
GetAll | 获取所有该类型的记录 |
GetById | 根据Id获取一条记录 |
GetByIds | 根据输入的多个Id获取多条记录 |
Store | 根据传入的实体,添加一条记录 |
StoreAll | 根据传入的实体集合,添加多条记录 |
Linq查询(针对于GetAll方法返回的IList<T>)示例:
public ActionResult Index() { Person p1 = new Person() { Id = 1, Name = "刘备" }; Person p2 = new Person() { Id = 2, Name = "关羽" }; Person p3 = new Person() { Id = 3, Name = "张飞" }; Person p4 = new Person() { Id = 4, Name = "曹操" }; Person p5 = new Person() { Id = 5, Name = "典韦" }; Person p6 = new Person() { Id = 6, Name = "郭嘉" }; List<Person> ListPerson = new List<Person>() { p2,p3,p4,p5,p6 }; using (IRedisClient RClient = prcm.GetClient()) { IRedisTypedClient<Person> IRPerson = RClient.As<Person>(); IRPerson.DeleteAll(); //------------------------------------------添加-------------------------------------------- //添加单条数据 IRPerson.Store(p1); //添加多条数据 IRPerson.StoreAll(ListPerson); //------------------------------------------查询-------------------------------------------- //Linq支持 Response.Write(IRPerson.GetAll().Where(m => m.Id == 1).First().Name); //刘备 //注意,用IRedisTypedClient的对象IRPerson的Srore()添加的才能用IRPerson()方法读取 Response.Write(IRPerson.GetAll().First(m => m.Id == 2).Name); //关羽 //------------------------------------------删除-------------------------------------------- IRPerson.Delete(p1); //删除 刘备 Response.Write(IRPerson.GetAll().Count()); //5 IRPerson.DeleteById(2); //删除 关羽 Response.Write(IRPerson.GetAll().Count()); //4 IRPerson.DeleteByIds(new List<int> { 3,4 }); //删除张飞 曹操 Response.Write(IRPerson.GetAll().Count()); //2 IRPerson.DeleteAll(); //全部删除 Response.Write(IRPerson.GetAll().Count()); //0 } return Content(""); }
另外,由于该接口并没有实现修改的方法,所以修改还得通过IRedisClient的实例:
public ActionResult Index() { PooledRedisClientManager prcm = new PooledRedisClientManager(new List<string>() { "127.0.0.1" }, new List<string>() { "127.0.0.1" }, RedisConfig); Person p1 = new Person() { Id = 1, Name = "刘备" }; Person p2 = new Person() { Id = 2, Name = "关羽" }; Person p3 = new Person() { Id = 3, Name = "张飞" }; Person p4 = new Person() { Id = 4, Name = "曹操" }; Person p5 = new Person() { Id = 5, Name = "典韦" }; Person p6 = new Person() { Id = 6, Name = "郭嘉" }; List<Person> ListPerson = new List<Person>() { p2,p3,p4,p5,p6 }; using (IRedisClient RClient = prcm.GetClient()) { IRedisTypedClient<Person> IRPerson = RClient.As<Person>(); IRPerson.StoreAll(ListPerson); //读取所有的Key List<string> ListKeys = IRPerson.GetAllKeys(); foreach (string key in ListKeys) { Response.Write(key + "<br/>"); } //修改的话只能通过Key修改 //urn:person:3 //urn:person:4 //urn:person:5 //ids:Person //urn:person:1 //urn:person:6 //urn:person:2 Person p7 = new Person() { Id = 8, Name = "撼地神牛" }; RClient.Set("urn:person:1", p7); Response.Write(IRPerson.GetAll().First(m => m.Id == 8).Name); //输出 撼地神牛 } return Content(""); }
原文:http://www.cnblogs.com/softwaredeveloper/p/4571315.html
内容总结
以上是互联网集市为您收集整理的Redis IRedisTypedClient全部内容,希望文章能够帮你解决Redis IRedisTypedClient所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。