2011-09-24 55 views
1

有人可以告訴我如何緩存我的webgrid結果,以便當我按列排序它不會重新運行我的存儲過程查詢時間?如何緩存mvc3 webgrid結果(以便列排序點擊不?)

當點擊一個列鏈接進行排序時,存儲過程(這是一個緩慢的),填充表/網格是每次重新執行,並擊中數據庫。任何緩存技巧和竅門將大大讚賞。

THX!

回答

2

那麼它應該調用查詢數據庫上你的資料庫的方法控制器動作內您可以檢查緩存是否已經包含結果。

這裏有一個常用的模式:

public ActionResult Foo() 
{ 
    // Try fetching the results from the cache 
    var results = HttpContext.Cache["results"] as IEnumerable<MyViewModel>; 
    if (results == null) 
    { 
     // the results were not found in the cache => invoke the expensive 
     // operation to fetch them 
     results = _repository.GetResults(); 

     // store the results into the cache so that on subsequent calls on this action 
     // the expensive operation would not be called 
     HttpContext.Cache["results"] = results; 
    } 

    // return the results to the view for displaying 
    return View(results); 
} 
+0

非常酷!謝謝! – JaJ