如何缓存 mvc3 webgrid 结果(以便 col sort click 不会?

发布于 2024-12-06 10:48:52 字数 136 浏览 0 评论 0原文

有人可以告诉我如何缓存我的网络网格结果,以便当我按列排序时,它不会每次都重新运行我的存储过程查询吗?

当单击列链接进行排序时,填充表/网格的存储过程(有点慢)每次都会重新执行并访问数据库。任何缓存提示和技巧将不胜感激。

谢谢!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-12-13 10:48:52

在调用存储库上应该查询数据库的方法的控制器操作内部,您可以检查缓存是否已包含结果。

这是一个常用的模式:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文