如何缓存MVC3的WebGrid结果(使COL排序点击不? [英] How to cache mvc3 webgrid results (so that col sort click doesn't?

查看:125
本文介绍了如何缓存MVC3的WebGrid结果(使COL排序点击不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我怎么可以缓存我的WebGrid结果让我排序列不会每次都重新运行我的存储过程的查询?

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?

当一列链接,点击排序,即填充表/网格存储的过程(这是一个有点慢)重新执行每次和访问数据库。任何缓存提示和技巧将大大AP preciated。

When clicking on a column link to sort, the stored proc (which is a little slow) that populates the table/grid is re-executed every time and hits the database. Any caching tips and tricks would be greatly appreciated.

THX!

推荐答案

那么它调用应该查询你可以检查缓存中是否已经包含了结果的数据库存储库的方法控制器动作中。

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

这篇关于如何缓存MVC3的WebGrid结果(使COL排序点击不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆