0

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.

3
  • I'd start with OutputCacheAttribute on your controller action. Commented Oct 22, 2015 at 20:37
  • @Jasen Thanks, I'm still a little confused about this. This method of caching seems to just cache the specific controller, but I don't really care about that controller because it's called via ajax and returns JSON. I want the object returned by the database and service (list of report object) to be in the cache, is that still possible? Commented Oct 23, 2015 at 12:10
  • It would cache an action result. Therefore, subsequent requests would not hit the database until the cache expires or is invalidated. If your C# code is written to use the object result then OutputCache may not be appropriate. Commented Oct 23, 2015 at 17:15

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks very much. The server side caching I was a little confused as if I have multiple users looking at reports and one of them tried to export a report to excel then wouldn't they potentially get a different cached object - basically the last report to be run by any user. Also with output caching this does sound like the best solution to me, I was a little concerned with the timing, as the user might wait a while before exporting it. Will I also see the same issue with multiple users or is it cached on a user by user basis?
You are probably passing userId to get the data. In that case, you can potentially cache the output to VaryByParam ="userId". This will create cache per user. You can also have the option to use OutputCacheLocation.Client. In the System.Runtime.Caching, your cache key should also include the userId so that the cache is per user
I'm still a little confused about this. This method of caching seems to just cache the specific controller, but I don't really care about that controller because it's called via ajax and returns JSON. I want the object returned by the database and service (list of report object) to be in the cache, is that still possible?
yes it is possible. I will post an example shortly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.