標籤:blog http io ar 使用 sp for strong on
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(""); }
ServiceStack.Redis 之 IRedisTypedClient<第四篇>