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