ASP.Net Caching

前端 未结 5 1789
再見小時候
再見小時候 2021-01-23 01:41

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 \"produc

5条回答
  •  北海茫月
    2021-01-23 02:16

    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;
    

提交回复
热议问题