缓存

1.       <%@ OutputCache Duration="10" VaryByParam="none" %>

缓存10秒,不启用参数

 

2.<%@ OutputCache Duration="10" VaryByParam="name" %>

缓存10秒。但如果url参数name参数不同,启用不同的缓存。

 

3.局部缓存

3.1 使用Substitution

<asp:Substitution ID="Substitution1" runat="server" MethodName="abc" />

public static String abc(HttpContext hc)

{

   return DateTime.Now.ToString();

}

3.2在用户自定义控件中加入<%@ OutputCache。哪么在使用用户控件的页面中,则只有用户控件被缓存

 

4.应用程序缓存

Cache key value形式不能设置时间,缓存一直存在

Cache.add()必须填写7个参数,不灵活

Cache.Insert (String, Object, CacheDependency, DateTime, TimeSpan)

String key

Object value

Cachedependency 依赖项

DateTime 强制过期时间,过多久就过期.如果使用强制过期,TimeSpan必须填写TimeSpan.zero

TimeSpan 从最后一次开始计时,多久过期(如果一直有人访问,就一直不过期)。如果使用绝对过期,哪么DateTime必须为DateTime.maxvalue

 

5.sqlcache 当数据库更新时,启用缓存

1-------------------注册库

Sql验证:aspnet_regsql -S localhost –U sa –P 123456 -d Pubs –ed

Windows验证:aspnet_regsql -S localhost –E -d Pubs –ed

 

上面的意思是,指定了本地的数据库服务器localhost,并指定了登陆的用户名和密码,并用参数-d指定了要采用哪一个数据库(这里是指定了pubs)数据库,-ed参数表示是允许该数据库使用sqlcachedependency功能。

 

2----------------------------注册表

Sql验证aspnet_regsql -S localhost –U sa -P 123456 -ed -d pubs -et -t authors

Window验证aspnet_regsql -S localhost –E -d pubs -et -t authors

 

3.页面<%@ OutputCache Duration="9999999" VaryByParam="none" SqlDependency="BB:UserInfo"  %>

 

4.配置文件

<connectionStrings>

           <add name="connstring" connectionString="Data Source=.;Initial Catalog=BB;Integrated Security=True"/>

            </connectionStrings>

<caching>

                 <sqlCacheDependency enabled="true" pollTime="500">

                      <databases>

                            <add name="BB" connectionStringName="connstring"/>

                       </databases>

                 </sqlCacheDependency>

           </caching>

原文地址:https://www.cnblogs.com/hun_dan/p/1594671.html