Reputation: 655
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.
Upvotes: 0
Views: 1948
Reputation: 286
I think there is a third option. I can think of 2 simple server side methods for caching that might work for you.
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
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
Upvotes: 3