介绍
存是在计算中广泛使用的一种技术,通过将经常访问的数据或存取开销较大的数据保留在内存或硬盘中来提高性能。在 Web 应用程序的上下文中,缓存用于在 HTTP 请求之间保留页或数据,在重用它们时可以不必耗费资源重新创建。


关键
1、@OutputCache指令中的属性:
    Duration - 缓存时间(秒)
    VaryByParam - 根据使用 POST 或 GET 发送的名称/值对来改变缓存的结果(多参数用分号隔开)
    VaryByControl - 根据用户控件中的控件来改变缓存的片段(值是控件ID,多控件用分号隔开)
    CacheProfile - 调用配置文件中设置的缓存时间

2、增加数据缓存时用Cache.Insert,可以指定缓存时间

3、替换缓存(Substitution)- 回调函数要是静态的

4、SqlCacheDependency
配置文件中的配置
ASP2.0 OUTCACHE 缓存使用收集~  <system.web>
ASP2.0 OUTCACHE 缓存使用收集~    
<caching>
ASP2.0 OUTCACHE 缓存使用收集~      
<sqlCacheDependency enabled="true" pollTime="轮询时间(毫秒)">
ASP2.0 OUTCACHE 缓存使用收集~        
<databases>
ASP2.0 OUTCACHE 缓存使用收集~          
<add name="名字" connectionStringName="连接字符串的名字" />
ASP2.0 OUTCACHE 缓存使用收集~        
</databases>
ASP2.0 OUTCACHE 缓存使用收集~      
</sqlCacheDependency>
ASP2.0 OUTCACHE 缓存使用收集~      
<!-- 如果是SqlServer2005的话,则只需如下设置,因为SqlServer支持基于通知的缓存失效
ASP2.0 OUTCACHE 缓存使用收集~      <sqlCacheDependency enabled="true" />
ASP2.0 OUTCACHE 缓存使用收集~      
-->
ASP2.0 OUTCACHE 缓存使用收集~    
</caching>
ASP2.0 OUTCACHE 缓存使用收集~  
</system.web>

如果不是SqlServer2005的话,应该使用aspnet_regsql注册一下
aspnet_regsql.exe -S "server" -E -d "database" -ed
aspnet_regsql.exe -S "server" -E -d "database" -et -t "table"
如果是Sql验证的话要把-E换成,-U(用户名),-P(密码)


示例
页面输出缓存
Cahce/Page.aspx

API操作缓存
Cahce/Page.aspx.cs
ASP2.0 OUTCACHE 缓存使用收集~using System;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Data;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Configuration;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Collections;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.Security;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.WebControls;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.WebControls.WebParts;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.HtmlControls;
ASP2.0 OUTCACHE 缓存使用收集~
ASP2.0 OUTCACHE 缓存使用收集~
public partial class Cahce_Page : System.Web.UI.Page
}

页面输出缓存(VaryByParam)
Cahce/PageByParam.aspx

上面页所需的web.config中的配置
ASP2.0 OUTCACHE 缓存使用收集~  <system.web>
ASP2.0 OUTCACHE 缓存使用收集~    
<caching>
ASP2.0 OUTCACHE 缓存使用收集~      
<outputCacheSettings>
ASP2.0 OUTCACHE 缓存使用收集~        
<outputCacheProfiles>
ASP2.0 OUTCACHE 缓存使用收集~          
<add name="CacheTest" duration="10" />
ASP2.0 OUTCACHE 缓存使用收集~        
</outputCacheProfiles>
ASP2.0 OUTCACHE 缓存使用收集~      
</outputCacheSettings>
ASP2.0 OUTCACHE 缓存使用收集~    
</caching>
ASP2.0 OUTCACHE 缓存使用收集~  
</system.web>

页面输出缓存(VaryByControl)
Cahce/CacheControl.ascx

Cahce/PageByControl.aspx

数据缓存
Cahce/Data.aspx

Cahce/Data.aspx.cs
ASP2.0 OUTCACHE 缓存使用收集~using System;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Data;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Configuration;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Collections;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.Security;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.WebControls;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.WebControls.WebParts;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.HtmlControls;
ASP2.0 OUTCACHE 缓存使用收集~
ASP2.0 OUTCACHE 缓存使用收集~
public partial class Cahce_Data : System.Web.UI.Page

替换缓存(部分区域强行不使用缓存)
Cahce/Substitution.aspx

Cahce/Substitution.aspx.cs
ASP2.0 OUTCACHE 缓存使用收集~using System;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Data;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Configuration;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Collections;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.Security;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.WebControls;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.WebControls.WebParts;
ASP2.0 OUTCACHE 缓存使用收集~
using System.Web.UI.HtmlControls;
ASP2.0 OUTCACHE 缓存使用收集~
ASP2.0 OUTCACHE 缓存使用收集~
public partial class Cahce_Substitution : System.Web.UI.Page

SqlCacheDependency
页的Sql缓存
Cahce/SqlCachePage.aspx

数据源控件的Sql缓存
Cahce/SqlCachePage.aspx

web.config中的相关配置
ASP2.0 OUTCACHE 缓存使用收集~  <connectionStrings>
ASP2.0 OUTCACHE 缓存使用收集~    
<add name="SqlConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\VS2005_Test.mdf;Integrated Security=True;User Instance=True"
ASP2.0 OUTCACHE 缓存使用收集~      providerName
="System.Data.SqlClient" />
ASP2.0 OUTCACHE 缓存使用收集~  
</connectionStrings>
ASP2.0 OUTCACHE 缓存使用收集~  
<system.web>
ASP2.0 OUTCACHE 缓存使用收集~    
<caching>
ASP2.0 OUTCACHE 缓存使用收集~      
<sqlCacheDependency enabled="true" pollTime="10000">
ASP2.0 OUTCACHE 缓存使用收集~        
<databases>
ASP2.0 OUTCACHE 缓存使用收集~          
<add name="VS2005_Test" connectionStringName="SqlConnectionString" />
ASP2.0 OUTCACHE 缓存使用收集~        
</databases>
ASP2.0 OUTCACHE 缓存使用收集~      
</sqlCacheDependency>
ASP2.0 OUTCACHE 缓存使用收集~      
<!-- 如果是SqlServer2005的话,则只需如下设置,因为SqlServer支持基于通知的缓存失效
ASP2.0 OUTCACHE 缓存使用收集~      <sqlCacheDependency enabled="true" />
ASP2.0 OUTCACHE 缓存使用收集~      
-->
ASP2.0 OUTCACHE 缓存使用收集~    
</caching>
ASP2.0 OUTCACHE 缓存使用收集~  
</system.web>


注意
Sql Server 2005 基于通知的缓存失效,不用aspnet_regsql设置,要设置属性SqlDependency="CommandNotification"。在首次执行某 SQL 查询之前,必须在应用程序某处调用 System.Data.SqlClient.SqlDependency.Start() 方法。此方法应放在 global.asax 文件的 Application_Start() 事件中。因为Sql Server 2005 基于通知的缓存失效对支持查询通知的查询语法有许多限制,所以我觉得最好先别用,而是使用轮询机制。在使用轮询机制时如本例子中的SqlCacheDependency="VS2005_Test:sqlcache",冒号前面是配置文件中配置的相关值指向数据库连接,后面是启用SqlCache的表名,注意区分大小写。


OK
[源码下载]

相关文章:

  • 2021-10-07
  • 2022-12-23
  • 2021-06-27
  • 2021-12-20
  • 2021-06-23
  • 2021-08-15
  • 2021-09-11
猜你喜欢
  • 2021-05-19
  • 2021-05-21
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
相关资源
相似解决方案