views:

170

answers:

5
+1  Q: 

ASP.Net Caching

I've got an application that downloads data from a 3rd party at 3am every morning Nothing changes in terms of content until then...

is it possible to cache the "product info" page until then? or is this something i should set in global.asax?

+1  A: 

Caching ASP.NET Pages

n8wrl
+1  A: 

You can set it on that page itself. In the code behind for that page:

Response.Cache.SetExpires("put tomorrow's date @ 3AM here");
Response.Cache.SetCacheability(HttpCacheability.Public);
AdaTheDev
+1  A: 

Yes you can cache it until then. There are many ways of doing this.

If you have a serverside call to retrieve the data then I would simply add this data to the cache when you first get it and set the expiration to be 3am the following day. Then on each page call check the cache for this data object and if it returns null, initiate another fetch of the data.

You can use page output cacheing too but this does not give you such detailed control.

something like this:

if (HttpContext.Current.Cache["MyData"] != null)
  return HttpContext.Current.Cache["MyData"] as DataObjectClass

//Get data into dataobject

HttpContext.Current.Cache.Add(
                  "MyData",
                  DataObject,
                  DateTime (tomorrow 3am),  // psuedo
                  null,
                  TimeSpan.Zero,
                  System.Web.Caching.CacheItemPriority.Normal,
                  null);

return DataObject;
Richard
doh! i hate being a slow typer :( this was what i ws going to do :)
Pure.Krome
+1  A: 

Another option is to use the System.Web.Caching.Cache class. Each time you load your data you can cache it here and then retrieve it as needed. This class does allow for expiration by TimeSpan but since you download the data at a specific time each day that doesn't really matter.

using System.Web.Caching;
Public Class SomeClass
{
  Public SomeDataCollection GetCachedData()
  {
      if( Cache["Key"] == null) //You want to always be sure to check if set
         Cache["Key"] = GetDataCollectionFromSomewhere();

      return Cache["Key"];
  }
}
William Edmondson
A: 

I would persist that 3rd party data every 24 hours. Caching it depends on what that data is. Is it a file that needs further processing? Then process it and cache it in memory. And your fail over goes like this: in-memory cache, temp persistent location, 3rd party location.