title: Approaches to caching large number of objects (ASP.NET cache vs static objects & caching objects individually vs as dictionary)
body: <p>I am writing a control that displays a list of items on the page. The database holds (lets say) 50,000 items which are related (many to many) with (lets say) 1000 pages.</p>
  
  <p>Instead of writing a stored procedure to return a set of complete items for a given page from the database (i.e. all columns, in order to hydrate a list of item objects), I am thinking of doing the following to render a list:</p>
  
  <ul>
  <li>on application startup, caching all listing items</li>
  <li>getting the SP to return just the key value for the items</li>
  <li>looping through the list of keys &amp; retrieving the matching object from cache</li>
  </ul>
  
  <p>Preliminary tests indicate this performs faster than hydrating a list from the DB for each page request. It also seems to cause less overall memory consumption, as each page request would just look up existing objects, as opposed to each request making a private list (when traffic is high and large pages are requested memory consumption gets very high).</p>
  
  <p>So now I am debating with fellow developers how to implement the cache. We have come up with several options, and this is where I would appreciate your opinions:</p>
  
  <ul>
  <li>Use the ASP.NET HttpContext.Current.Cache</li>
  <li>Use static/singleton objects</li>
  </ul>
  
  <p>Within each of these there is also a choice whether to:</p>
  
  <ol>
  <li>Cache each item object individually</li>
  <li>Build a dictionary (or similar list) containing all item objects then cache the dictionary</li>
  </ol>
  
  <p>Read performance is the main concern. Maintenance is a secondary concern - we would write our GetItems methods defensively to check if each item is in the cache and if not, retrieve it from the DB and insert it into the cache.</p>
  
  <p>I know there is no right answer ... there are pros &amp; cons for everything. I am just looking to see if people have pitfalls they can share about any of the above, or if any approach stands out as being by far the best performing, or is particularly hard to merge new data into.</p>
  
  <p>Thanks</p>