The query below retrieves the CustomerId and NumOrders from the Order entity and pass the result at Cache_Individual_Orders_Items function which iterates the collection and put the individual items in cache having a prefix of "Order:CustomerId:".
After I put the items in cache how to grab the individual items having NumOrders value of more than 100+ from cache?
public CustOrderList() {
var ordersList = from p in dbNOrthwind.Orders
select new {
CustomerId = p.CustomerId,
ProductId = p.ProductId,
NumOrders = p.OrderDetails.Count
}
Cache_Individual_Orders_Items(ordersList); //put items in cache
}
public void Cache_Individual_Orders_Items(IList<Order> ordersList)
{
Cache cache = HttpRuntime.Cache;
DateTime absolutionExpiration = Cache.NoAbsoluteExpiration;
TimeSpan slidingExpiration = Cache.NoSlidingExpiration;
foreach (Order order in ordersList)
{
string key = "Order:CustomerId:" + order.CustomerId + ":ProductId" + order.ProductId;
cache.Add(key, order, absolutionExpiration, slidingExpiration, priority);
}
}
basically you have use Dictionary and have a key concatination CustomerID_ProductID_NumOrders and check if already contail the key if not then query and cache and return from cache
First you have to retrieve all the records stored in the Cache. Once it's done, you may have to retrieve all the records which has
NumOrders more than 100, by using LINQ query etc.,
imperialx
Member
523 Points
1220 Posts
Grab Collection from Cache
Jul 15, 2012 10:43 AM|LINK
How to grab items from cache collection?
The query below retrieves the CustomerId and NumOrders from the Order entity and pass the result at Cache_Individual_Orders_Items function which iterates the collection and put the individual items in cache having a prefix of "Order:CustomerId:".
After I put the items in cache how to grab the individual items having NumOrders value of more than 100+ from cache?
public CustOrderList() { var ordersList = from p in dbNOrthwind.Orders select new { CustomerId = p.CustomerId, ProductId = p.ProductId, NumOrders = p.OrderDetails.Count } Cache_Individual_Orders_Items(ordersList); //put items in cache } public void Cache_Individual_Orders_Items(IList<Order> ordersList) { Cache cache = HttpRuntime.Cache; DateTime absolutionExpiration = Cache.NoAbsoluteExpiration; TimeSpan slidingExpiration = Cache.NoSlidingExpiration; foreach (Order order in ordersList) { string key = "Order:CustomerId:" + order.CustomerId + ":ProductId" + order.ProductId; cache.Add(key, order, absolutionExpiration, slidingExpiration, priority); } }Thank You,
-imperialx
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Grab Collection from Cache
Jul 15, 2012 11:09 AM|LINK
basically you have use Dictionary and have a key concatination CustomerID_ProductID_NumOrders and check if already contail the key if not then query and cache and return from cache
imperialx
Member
523 Points
1220 Posts
Re: Grab Collection from Cache
Jul 15, 2012 11:46 AM|LINK
What if I change the key to this
It only has the CustomerID. How can I still get the NumOrders from the dictionary?
roopeshreddy
All-Star
20119 Points
3320 Posts
Re: Grab Collection from Cache
Jul 16, 2012 09:47 AM|LINK
Hi,
First you have to retrieve all the records stored in the Cache. Once it's done, you may have to retrieve all the records which has NumOrders more than 100, by using LINQ query etc.,
Hope it helps u...
Roopesh Reddy C
Roopesh's Space