简体   繁体   English

ASP.Net缓存

[英]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... 我有一个应用程序,每天凌晨3点从第三方下载数据,直到那时内容的内容都没有改变...

is it possible to cache the "product info" page until then? 在此之前是否可以缓存“产品信息”页面? or is this something i should set in global.asax? 还是我应该在global.asax中设置的东西?

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. 如果您有服务器端调用来检索数据,那么我将在首次获取数据时将其添加到缓存中,并将过期时间设置为第二天凌晨3点。 Then on each page call check the cache for this data object and if it returns null, initiate another fetch of the data. 然后在每个页面调用中检查此数据对象的缓存,如果它返回null,则启动另一个数据获取。

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;

Another option is to use the System.Web.Caching.Cache class. 另一个选择是使用System.Web.Caching.Cache类。 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. 此类确实允许TimeSpan到期,但是由于您每天都在特定时间下载数据,所以这并不重要。

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"];
  }
}

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);

I would persist that 3rd party data every 24 hours. 我会每24小时保留一次第三方数据。 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. 您的故障转移将是这样的:内存缓存,临时持久位置,第三方位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM