9

I have an MVC 3 c# website running which pulls records from a webservice. As the dataset it gets from the webservice becomes bigger and bigger, I'm searching for a way that creating the cache of it isn't triggered by the first user to visit the site while there is no current cache, but on a daily schedule (like a cron job, scheduled task or something).

How should I do this? Do I need some sort of trigger library like Quartz.net ? (I'd rather use a simpler solution)

What I have in my controller now is:

private List<DataSummary> GetSummaries()
    {
        //get summaries from cache if available
        List<DataSummary> summaries = (List<DataSummary>)HttpContext.Cache["SummariesCache"];
        if (summaries == null)
        {
            //cache empty, retrieve values
            summaries = _webservice.GetSummaries();
            //cache it
            HttpContext.Cache.Add("SummariesCache", summaries, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
        }
        return summaries;

    }

Edit

using CacheItemRemovedCallback causes time-out errors

Daniël Tulp
  • 1,745
  • 2
  • 22
  • 51

1 Answers1

2

This is a bit of a hacky solution but you could always create a scheduled task on the server which runs an executable. This executable hits a particular URL on your website, which in turn populates the cache exactly as you've outlined above. To hit the URL in the executable you'd need something along these lines:

var req = (HttpWebRequest)WebRequest.Create(URL);
req.Timeout = 30000;        
req.KeepAlive = false;
req.Method = "GET";
var res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.OK){
    // Your cache should be refreshed
}

Creating a scheduled task is pretty simple as well - just make sure you run it in a user account that has execute permissions on the exe you create and don't require the user to be logged on for the task to start.

Edit: Added a guide to setting up a scheduled task on Windows Server 2008

  1. Open the start menu.
  2. Type "Task Scheduler" in the search box (in older versions of Windows I believe this was found in Administrative Tools as "Scheduled Tasks"). Open the Task Scheduler result.
  3. Right click on the root node of the tree on the left - "Task Scheduler (local)" - and select "Create Basic Task".
  4. Give the task a name and description so you can identify it later. Click next.
  5. Select your duration (in your case daily sounds about right). Click next.
  6. Change the start time if you want to. Click next.
  7. Leave the action as "Start a program". Click next.
  8. Click "Browse..." and find the executable you created. Click next.
  9. On the last screen, tick the box that says "Open the Properties dialog..." then Finish.
  10. Change the security options to "Run whether user is logged on or not". If necessary, change the user or group to an appropriate user account (one with necessary permissions to run the exe).
  11. Click OK. You will be asked to enter the account password that will run the task. Do this and click OK.
  12. Click on the Task Scheduler Library on the left. Your task should appear in the list on the right. You can test it by right clicking and selecting Run.
Maloric
  • 5,525
  • 3
  • 31
  • 46
  • I've never worked with an executable on in an MVC website, sounds odd. Or am I missing something here? You say it is simple to create a scheduled task. Do you have an example? – Daniël Tulp Feb 25 '13 at 10:15
  • The executable isn't part of the website as such, you would just create a standalone executable and put it on the server outside of the web root. As for creating the scheduled task, I will edit my answer with a guide for Windows Server 2008 (this is assuming you have remote desktop access to your server). – Maloric Feb 25 '13 at 11:21
  • thank you for your detailed guide, unfortunately, I do not have remote access, but a +1 anyway for I know someone else will need this answer – Daniël Tulp Feb 25 '13 at 14:32
  • I suppose you could set up a scheduled task on your own machine to do this (since you're just hitting a page on your site anyway). The downside is that your PC won't always be on. – Maloric Feb 25 '13 at 20:02
  • 1
    then how about a service like: http://www.mywebcron.com/, I don't need a executable then right? Just point the cron to that URL and it should work. I have a test set up for this evening – Daniël Tulp Feb 25 '13 at 20:42
  • I was thinking precisely that, as I occasionally need this sort of thing myself. I will set up a service next week and share the URL here. – Maloric Feb 26 '13 at 07:30
  • I use http://mywebcron.com/ to visit the website daily now at 01:00 am and it seems to work. I'll accept your answer! – Daniël Tulp Feb 26 '13 at 07:30