9.3.5 数据高速缓存 首先需要注意的是,数据高速缓存与记录集高速缓存虽然都用于改善性能,但两者是无关的。数据高速缓存是临时的数据存储区,允许使用高速缓存中的数据,而不是重新生成新的数据。这只适用于那些不经常改动但多次被访问的数据。 在ASP中一个最简单的缓存数据的方法是使用Application和Session范围的变量。例如,假设有一些需要选择书类型的网页。正常情况下,可能会创建一个含有以下函数的包含文件。 <% Function BookTypes() Dim rsBookTypes Dim strQuote strQuote = Chr(34) Set rsBookTypes = Server.CreateObject ("ADODB.Recordset") ' Get the book types rsBookTypes.Open "usp_BookTypes", strConn Response.Write "" rsBookTypes.Close Set rsBookTypes = Nothing End Function %> 这仅仅是调用一个存储过程,从而得到书的类型,同时创建一个SELECT列表。上述代码的缺点在于每次调用该函数都必须访问数据库。因此,重新修改这个函数。 <% Function BookTypes() Dim rsBookTypes Dim strQuote Dim strList ' See if the list is in the cache strList = Application("BookTypes") If strList = "" Then ' Not cached, so build up list and cache it strQuote = Chr(34) Set rsBookTypes = Server.CreateObject ("ADODB.Recordset") ' Get the book types rsBookTypes.Open "usp_BookTypes", strConn strList = "" rsBookTypes.Close Set rsBookTypes = Nothing ' Check the list Application("BookTypes") = strList End If