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(""); }