views:

17

answers:

1

Hey guys,

I'm currently thinking about caching most of my viewdata excpt user specific data after a user logs on. I thought the simplest way was caching the ViewData object itself and adding the user specific data after it was loaded. Are there any downsides of this approach? Are there better ways?

string cacheKey = "Nieuws/show/" + id;
if (HttpRuntime.Cache[cacheKey] != null)
{
      ViewData = HttpRuntime.Cache[cacheKey] as ViewDataDictionary;
}
else
{
      // add stuff to view data

      HttpRuntime.Cache.Insert(cacheKey, ViewData, null, DateTime.Now.AddSeconds(180), Cache.NoSlidingExpiration,
      CacheItemPriority.NotRemovable, null);
}
A: 

Mmhhh... The ViewData could also consist of many unneed informations you are not interested in cacheing. I would suggest to created a fitting model-class and persist this in your session-state. This would give you a clearer architecture to work with. Maybe also some additional timestamp to have kind of updating-scenario supported to reload data from db...

Olaf Watteroth