Frage

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

War es hilfreich?

Lösung

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.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top