65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET中的缓存

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.86/5 (36投票s)

2002年4月18日

2分钟阅读

viewsIcon

161342

downloadIcon

1782

演示在ASP.NET中的数据缓存

引言

ASP.NET 有内置的缓存引擎, ASP.NET 页面用它来跨 HTTP 请求存储和检索对象。ASP.NET 缓存对每个应用程序是私有的,并在内存中存储对象。缓存的生命周期等同于应用程序的生命周期;也就是说,当应用程序重新启动时,将重新创建缓存。

将一个项目放入缓存中的方法如下

Cache ("key") = Value

检索数据同样简单

Value = Cache("key")
If Value <> Null Then
    DisplayData(Value)
End If

对于 ASP.NET 应用程序中的复杂功能,缓存支持回收、过期以及文件和键依赖项。

  • 回收意味着如果内存变得稀缺,缓存会尝试删除不经常使用或不重要的项目。
  • 过期允许程序员为缓存项目设置生命周期,可以是显式的(例如,在 12:00 过期),也可以是相对于项目的最后一次使用时间(例如,在最后一次访问项目 10 分钟后过期)。在项目过期后,它将从缓存中删除,并且将来尝试检索它将返回 null 值,除非该项目被重新插入缓存。
  • 文件和键依赖项允许缓存项目的有效性基于外部文件或其他缓存项目。如果依赖项发生变化,则缓存项目将被失效并从缓存中删除。以下是一个您可能使用此功能的示例:一个应用程序从定期更新的 XML 文件中读取财务信息。应用程序处理文件中的数据并创建一个对象图,这些对象表示该数据,并采用可消费的格式。应用程序缓存该数据,并插入对读取数据的文件的依赖项。当文件更新时,数据将从缓存中删除,应用程序可以重新读取它并重新插入数据的更新副本。

数据缓存示例

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
 
<html>
  <script language="VB" runat="server">
 
    Sub Page_Load(Src As Object, E As EventArgs)
 
        Dim dataSource As DataView
 
        ' Retrieve item from cache if anything is not there then we have to
        ' add to the cache 
        dataSource = Cache("CheDataSet")
 
        If dataSource Is Nothing
 
            Dim Conn As SqlConnection
            Dim Cmd As SqlDataAdapter
 
            Conn = New SqlConnection(_
                           "server=database;uid=sa;pwd=;database=Northwind")
            Cmd = New SqlDataAdapter("select * from customers", Conn)
 
            Dim ds As New DataSet
            Cmd.Fill(ds, "dsUsers")
 
            dataSource = New DataView(ds.Tables("dsUsers"))
            Cache("CheDataSet") = dataSource
 
            CacheMsg.Text = "Dataset was Created directly from the Datebase. " &_
                            "Here the Cache was not used but the Cache was " &_
                            "Created, so check for the performance next time."
        Else
            
            cacheMsg.Text = "Dataset was Retrieved from cache which was " &_
                            "created earlier."
        End If
 
        MyDataGrid.DataSource=dataSource
        MyDataGrid.DataBind()
    End Sub
 
  </script>
 
  <body>
 
    <form method="GET" runat="server">
 
      <h3><font face="Verdana">Caching Data Example</font></h3>
        <i><asp:label id="CacheMsg" runat="server"/></i>
 
      <ASP:DataGrid id="MyDataGrid" runat="server"
        Width="700"
        BackColor="#Eaccff"
        BorderColor="black"
        ShowFooter="false"
        CellPadding=3
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
       HeaderStyle-BackColor="#ffaad" />
 
      <p>
    </form>
  </body>
</html>

示例

1. 第一次执行时

2. 在创建缓存后执行时

结论

  1. 只有在需要时才应非常小心地使用数据缓存。
  2. 数据缓存允许以编程方式缓存对象。
  3. 缓存的作用域限定于应用程序,其生命周期等同于应用程序的生命周期。

许可证

本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。

作者可能使用的许可证列表可以在此处找到。

© . All rights reserved.