1.通过时间来控制缓存(一旦时间到了,无论数据库的数据是否改变,都会释放缓存)
<%@ outputcache Druation="60" VaryByParam="none"%>
将页面缓存为60秒 60秒内刷新内容不变 提高访问效率
由参数改变缓存内容:
<%@ outputcache Druation="60" VaryByParam="state"%>
VaryByParam="state"
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString2 %>"
SelectCommand="SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract FROM authors where state=@state">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="CA" Name="state" QueryStringField="state" />
</SelectParameters>
</asp:SqlDataSource>
同过指定不同的state来缓存state内容
<%@ outputcache Druation="60" VaryByParam="state"%>
<asp:QueryStringParameter DefaultValue="CA" Name="state" QueryStringField="state" />
联系起来
回调缓存:
<script runat="server">
Shared Function GetCurrentDate(ByVal context As HttpContext) As String
Return Now.ToString()
End Function
</script>
Real Time:
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />
也可以把一个web用户控件 设置为 缓存,然后把用户控件添加到别的页面中
这样这个页面的本身 并没有设置缓存 但是其中的控件却是缓存起来了 这样可以达到同样的效果
2.通过数据库(通过配置数据库连接池,只有当数据库中数据改变的时候,缓存才会改变)