禁用asp.net缓存

Ale*_*dre 5 asp.net caching

有没有办法在web.config或global.asax上以某种方式禁用Cache(System.Web.Caching.Cache,而不是aspx页面的OutPut缓存)?

cod*_*net 4

来自 MSDN,

缓存 API 配置设置

您可以在 Web.config 文件中配置应用程序的缓存 API。与页面输出缓存一样,应用程序托管者可以在 Machine.config 文件中设置配置属性,并锁定所有应用程序的缓存配置设置。应用程序缓存 API 在 CacheSection 中配置。

您可以通过为配置文件的 CacheSection 中的 DisableExpiration 和 DisableMemoryCollection 等属性分配值来指定应用程序缓存 API 配置设置。

如果DisableMemoryCollection属性设置为true,则对缓存相关API的调用将不起作用。

警告:如果DisableMemoryCollection 属性设置为true,则缓存不会尝试收集未使用的项目。使用此设置时请务必小心,因为禁用内存收集可能会很快导致应用程序出现内存不足的情况。

您可以在 web.config 中设置它或以编程方式执行此操作,

// Get the application configuration file.
   System.Configuration.Configuration config =
   System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");


   System.Web.Configuration.CacheSection cacheSection =
   (System.Web.Configuration.CacheSection)config.GetSection(
    "system.web/caching/cache");

  cacheSection.DisableMemoryCollection = true;

  // Save the configuration file.
  config.Save(System.Configuration.ConfigurationSaveMode.Modified);   
Run Code Online (Sandbox Code Playgroud)