/// <summary> /// 根据lambada表达式删除符合条件的实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="listId"></param> /// <param name="func"></param> public void RemoveEntityFromList <T>(string listId, Func <T, bool> func) { IRedisTypedClient <T> iredisClient = Redis.As <T>(); IRedisList <T> redisList = iredisClient.Lists[listId]; T value = redisList.Where(func).FirstOrDefault(); redisList.RemoveValue(value); iredisClient.Save(); }
/// <summary> /// To get list of menuitems in the Restaurant /// </summary> /// <param name="restaurantId"></param> /// <param name="searchText"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns>IEnumerable<RestaurantMenu></returns> public async Task <IEnumerable <RestaurantMenu> > GetRestaurantMenusAsync(int restaurantId, string searchText = null, int pageIndex = 0, int pageSize = 0) { LoggerManager.InfoLog(string.Format("Executing service to get the menu list of restaurant - {0}", restaurantId)); IEnumerable <RestaurantMenu> searchList = null; LoggerManager.DebugLog("Establishing connection with Redis cache"); if (restaurantId > 0) { //Read Redis Cache IRedisList <RestaurantMenu> menuList = getRedisCacheObject(_configuration["CacheKeyPrefix"] + restaurantId); //Load Redis Cache if (menuList == null || menuList.Count == 0) { LoggerManager.DebugLog("Loading items into the Redis cache"); menuList = await LoadMenuToRedisCache(restaurantId.ToString()); } //Search cache for menuitem and return search list if (menuList != null) { if (!String.IsNullOrEmpty(searchText)) { LoggerManager.InfoLog(string.Format("Searching for menu items with text - {0}", searchText)); searchList = menuList.Where <RestaurantMenu>(menuitem => menuitem.MenuItemName.Contains(searchText.ToUpper())); } else { searchList = menuList.AsEnumerable <RestaurantMenu>(); } } } if (pageIndex > 0 && pageSize > 0 && searchList != null && searchList.Count() > 0) { searchList = searchList.Skip(pageSize * (pageIndex - 1)).Take(pageSize); LoggerManager.InfoLog(string.Format("Returning paged menu items of page index - {0} and of size {1}", pageIndex, pageSize)); if (searchList == null || searchList.Count() <= 0) { throw new Exception("End of Page"); } } return(searchList); }
static void Main(string[] args) { string host = "localhost"; int port = 6379; string elementKey = "testKeyRedis"; using (RedisClient redisClient = new RedisClient(host, port)) { if (redisClient.Get <string>(elementKey) == null) { redisClient.Set(elementKey, "some cached value"); } Console.WriteLine("Item value is: {0}", redisClient.Get <string>(elementKey)); IRedisTypedClient <Phone> phones = redisClient.As <Phone>(); Phone phoneFive = phones.GetValue("5"); if (phoneFive == null) { phoneFive = new Redis.Phone { Id = 5, Manufacturer = "Apple", Model = "xxxx", Owner = new Person { Id = 1, Age = 90, Name = "OldOne", Profession = "teacher", Surname = "SurName" } }; } phones.SetValue(phoneFive.Id.ToString(), phoneFive); Console.WriteLine("Phone model is: {0}, Phone Owner Name is: {1}", phoneFive.Manufacturer, phoneFive.Owner.Name); IRedisList <Phone> mostSelling = phones.Lists["urn:phones:mostselling"]; IRedisList <Phone> oldCollection = phones.Lists["urn:phones:oldCollection"]; Person phoneOwner = new Person { Id = 7, Age = 90, Name = "OldOne", Profession = "teacher", Surname = "SurName" }; mostSelling.Add(new Phone { Id = 5, Manufacturer = "Apple", Model = "54321", Owner = phoneOwner }); oldCollection.Add(new Phone { Id = 8, Manufacturer = "Moto", Model = "111111", Owner = phoneOwner }); var upgradedPhone = new Phone { Id = 5, Manufacturer = "LG", Model = "12345", Owner = phoneOwner }; mostSelling.Add(upgradedPhone); Console.WriteLine("Phones in mostSelling list:"); foreach (Phone ph in mostSelling) { Console.WriteLine(ph.Id); } Console.WriteLine("Phones in oldCollection list:"); foreach (Phone ph in oldCollection) { Console.WriteLine(ph.Id); } oldCollection.Remove(upgradedPhone); IEnumerable <Phone> LGPhones = mostSelling.Where(ph => ph.Manufacturer == "LG"); foreach (Phone ph in LGPhones) { Console.WriteLine("LG phone Id: {0}, LG phone Model: {1}", ph.Id, ph.Model); } Phone singleElement = oldCollection.FirstOrDefault(ph => ph.Id == 8); Console.WriteLine("singleElement phone Id: {0}, singleElement phone Model: {1}", singleElement.Id, singleElement.Model); phones.SetSequence(0); redisClient.Remove("urn:phones:mostselling"); redisClient.Remove("urn:phones:oldCollection"); Console.WriteLine("urn:phones:mostselling Count: {0}", phones.Lists["urn:phones:mostselling"].Count); } Console.ReadKey(); }
public ActionResult Index() { string message = string.Empty; string host = "localhost"; using (var redisClient = new RedisClient(host)) { //Create a 'strongly-typed' API that makes all Redis Value operations to apply against Phones IRedisTypedClient <Phone> redis = redisClient.As <Phone>(); //Redis lists implement IList<T> while Redis sets implement ICollection<T> IRedisList <Phone> mostSelling = redis.Lists["urn:phones:mostselling"]; IRedisList <Phone> oldCollection = redis.Lists["urn:phones:oldcollection"]; Person phonesOwner = new Person { Id = 7, Age = 90, Name = "OldOne", Profession = "sportsmen", Surname = "OldManSurname" }; // adding new items to the list mostSelling.Add(new Phone { Id = 5, Manufacturer = "Sony", Model = "768564564566", Owner = phonesOwner }); mostSelling.Add(new Phone { Id = 8, Manufacturer = "Motorolla", Model = "324557546754", Owner = phonesOwner }); var upgradedPhone = new Phone { Id = 3, Manufacturer = "LG", Model = "634563456", Owner = phonesOwner }; mostSelling.Add(upgradedPhone); // remove item from the list oldCollection.Remove(upgradedPhone); // find objects in the cache IEnumerable <Phone> LGPhones = mostSelling.Where(ph => ph.Manufacturer == "LG"); // find specific Phone singleElement = mostSelling.FirstOrDefault(ph => ph.Id == 8); //reset sequence and delete all lists redis.SetSequence(0); redisClient.Remove("urn:phones:mostselling"); redisClient.Remove("urn:phones:oldcollection"); } ViewBag.Message = message; return(View()); }
/// <summary> /// 查询单条数据 /// </summary> /// <param name="func">查询条件</param> /// <returns></returns> public TEntity Find(Func <TEntity, bool> func) { return(table.Where(func).FirstOrDefault()); }