How to cache mvc3 webgrid results (so that col sort click doesn't?

后端 未结 1 1200
春和景丽
春和景丽 2021-01-16 04:03

Can somebody please tell me how I can cache my webgrid results so that when I sort by column it doesn\'t re-run my stored procedure query every time?

When clicking

1条回答
  •  悲哀的现实
    2021-01-16 04:18

    Well inside the controller action which is invoking the method on your repository supposed to query the database you could check whether the cache already contains the results.

    Here's a commonly used pattern:

    public ActionResult Foo()
    {
        // Try fetching the results from the cache
        var results = HttpContext.Cache["results"] as IEnumerable;
        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 讨论(0)
提交回复
热议问题