0

I am trying to figure out the best way to cache and retrieve individual elements in a collection.
The code below illustrates how I am able to cache the list itself successfully.
But when it comes to certain elements in the list- ContesaProperties - I am unable to deduce an efficient and clean way to do it.

The second part of the code below ---- pseudo-code ---- forms the core of my question.

//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
}
leppie
  • 115,091
  • 17
  • 196
  • 297
GilliVilla
  • 4,998
  • 11
  • 55
  • 96
  • Use [Memory Cache .Net 4.0](http://stackoverflow.com/questions/11729023/memory-cache-net-4-0-performance-test-astonishing-result) – lsalamon May 06 '13 at 16:00
  • The only way is to get the list and iterate through each for finding out the properties that you require. What you have done is the efficient way IMHO. – Saravanan May 12 '13 at 13:24

0 Answers0