C# 在集合中缓存元素的有效方法
我试图找出缓存和检索集合中单个元素的最佳方法。C# 在集合中缓存元素的有效方法,c#,.net,asp.net-mvc,caching,collections,C#,.net,Asp.net Mvc,Caching,Collections,我试图找出缓存和检索集合中单个元素的最佳方法。 下面的代码说明了如何成功缓存列表本身。 但是,当涉及到列表中的某些元素(contesaproperty)时,我无法推断出一种高效、干净的方法来完成它 下面代码的第二部分——伪代码——构成了我问题的核心 //Define Cache Keys private const string CACHE_CONTESALIST_KEY = "ContesaList"; private const string CACHE_CONTESA_PROPERTY_K
下面的代码说明了如何成功缓存列表本身。
但是,当涉及到列表中的某些元素(contesaproperty)时,我无法推断出一种高效、干净的方法来完成它 下面代码的第二部分——伪代码——构成了我问题的核心
//Define Cache Keys
private const string CACHE_CONTESALIST_KEY = "ContesaList";
private const string CACHE_CONTESA_PROPERTY_KEY = @"ContesaProperty-{0}";
.....
.....
NameValueCollection namevaluecollection = currentContext.GetCollection();
ContesaList contesaList = null;
if (Cache[CACHE_CONTESALIST_KEY] != null)
{
contesaList = Cache[CACHE_WEBCLASSLIST_KEY] as ContesaList;
}
if (contesaList == null)
{
contesaList = ContesaList.GetWebClassList();
Cache.Insert(CACHE_CONTESALIST_KEY, contesaList);
}
var contesa = contesaList.First(c => c.Id.Equals(namevaluecollection["contesaElements"], StringComparison.OrdinalIgnoreCase));
if (contesa != null)
{
// Check for Contesa Properties in cache(How?);
// if not available cache the properties within the collection where each key is
// is suffixed with the property id.
// Begin Pseudo-Code
// if (thePropertiesNotInCache)
// {
// This takes care of first element but how to do it for all the elements?
// Cache.Insert(String.Format(CACHE_CONTESA_PROPERTY_KEY, contesa.ContesaProperties.ToList()[0].CPropertyId), webclass.ContesaProperties.ToList()[0]);
// }
// End Pseudo-Code
}
唯一的方法是获取列表并遍历每个列表,以找到所需的属性。你所做的是有效的方法。