2010-06-11 159 views
0

我目前正在考慮在用戶登錄後緩存大部分viewdata excpt用戶特定數據。我認爲最簡單的方法是緩存ViewData對象本身並在加載後添加用戶特定的數據。這種方法有什麼缺點嗎?有更好的方法嗎?緩存asp.net viewdata

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); 
} 

回答

1

mmhhh ... ViewData也可能包含許多您不需要緩存的不需要的信息。我會建議創建一個合適的模型類,並在會話狀態中堅持這一點。這會給你一個更清晰的架構來處理。也許還有一些額外的時間戳有更新的情況下支持從數據庫重新加載數據...