python小菜 2015-10-22 20:03 采纳率: 0%
浏览 40

在MVC .NET中进行缓存

I am displaying a table based on a query from a database. An ajax request is used to get the data from the table, serialise it into json and by using datatables JavaScript library on the front end, the data is updated.

I have a requirement to be able to then output that table to Excel. This needs to be in a very specific format for which I have written the class etc for in C# which correctly works. I also have the controller which will then pass back a filestream so the user can download the file.

My question is around caching the data from the database. I don't want to have to go back to the database and re run the query to then pass to my excel exporter the report, it should be stored in some way - the latest report that user has run.

There are two ways I can think of doing this:

1) client caching - store the latest report run by the user and then have the excel exporter use the latest report data from the cache.

2) send back the latest json response from the Ajax call and deserialise in the controller back into a report object.

Number one seems the most logical as two seems messy and prone to error. I'd like to know if this is the best solution and if not a suggestion on what method would be best.

  • 写回答

1条回答 默认 最新

  • weixin_33709364 2015-10-22 20:45
    关注

    I think there is a third option. I can think of 2 simple server side methods for caching that might work for you.

    1. Output Caching
    2. System.Runtime.Caching

    Output caching is quite useful and extremely easy if you set it on the action

    In the example below the action will be cached for 10 sections

    public class HomeController : Controller
    {
        [OutputCache(Duration=10, VaryByParam="none")]
        public ActionResult Index()
        {
            return View();
        }
    
    }
    

    You could even have the caching key based on a parameter

     [OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
        public ActionResult Details(int id)
        {
            ViewData.Model = _dataContext.Movies.SingleOrDefault(m => m.Id ==    id);
            return View();
        }
    

    Here is a link to output caching

    http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/improving-performance-with-output-caching-cs

    If you however want to cache on the server side with System.Runtime.Caching you can find a nice example here http://deanhume.com/home/blogpost/object-caching----net-4/37

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?