2011-09-24 6 views
1

누군가 내 webgrid 결과를 캐시하여 어떻게 열별로 정렬 할 때마다 내 저장된 프로 시저 쿼리를 다시 실행하지 못하게 할 수 있습니까? 시간?mvc3 webgrid 결과를 캐시하는 방법 (col 정렬을 클릭하지 않도록?

정렬 할 열 링크를 클릭하면 테이블/그리드를 채우는 저장된 proc (약간 느림)이 매번 다시 실행되고 데이터베이스에 도달합니다. 캐싱 요령 및 트릭은 크게 감사합니다.

들으! 데이터베이스를 쿼리하기로 저장소에 메소드를 호출하는 컨트롤러 액션 내부에 잘

답변

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

관련 문제