It has been 7 years since ASP was released, and the use of ASP technology has become quite mature. Since Microsoft launched ASP.NET, it has gradually stopped updating the ASP version. But since many people are still accustomed to using ASP to develop websites, once again I will use a simple example to illustrate how to use Cache in ASP.
Simply put, the basic principle of using Cache is to store frequently needed and expensive data in memory for a certain period of time so that these data can be directly and globally accessed. For example, there is some data that needs to be queried from multiple tables in the database, and almost every page needs to call this data. The best implementation in this case is to cache this part of the data. A simple implementation in ASP is to encapsulate the final expression form of these data (such as HTML stream) in a string and then store it in the ASP built-in object Application (this article mainly What is discussed is dynamic Cache, and simple ASP applications will be omitted). The advantage of this is that this HTML can be called globally throughout the entire website, and the Application is stored in memory, so there is no need to query the database, thus speeding up the response time and saving server load. Of course, this comes at the expense of memory consumption and is a typical example of exchanging space for time.
Although there are many benefits to using this method, when encountering frequently changing data sources (databases), this method may no longer be applicable, because the ASP Application object has a disadvantage, that is, it cannot automatically change with changes in the data source. change, or control the refresh interval. Therefore, developers need to program to implement dynamic Cache. Of course, during program design, the Application can be updated every time the data source (database) is changed. Thus the data source (database) is always consistent. There will be more issues to consider in programming, and it is easy to miss details. So I don't recommend this method except in specific situations.
I think the best way in ASP is to use programming to regularly refresh the Cache, which means setting an expiration time for the data stored in the Application. Of course, the Application object in ASP does not have such an ExpireTime attribute. This needs to be implemented programmatically.
Code
ASP: default.asp
< %@Language=VBScript% >
<%Option Explicit%>
<%Response.Buffer=True%>
<!--#include file = "conn.asp"-->
<!--#include file = "GetCache.asp"-->
<HTML>
<HEAD>
<TITLE>ASP Cache Demonstration</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
</HEAD>
<BODY>
<h4>Refresh Cache every 10 seconds:</h4>
<%
response.Flush
GetHTMLStream
response.Write
HTMLStream
%>
</body>
</html>
ASP:getcache.asp
<%
Const CACHE_DEFAULT_INTERVAL = 30 'Refresh cache every 30 seconds
Dim HTMLStream
DimIsExpires
IsExpires = CacheExpires
FunctionCacheExpires
Dim strLastUpdate
Dim result strLastUpdate = Application("LastUpdate")
If (strLastUpdate = "") Or (CACHE_DEFAULT_INTERVAL < DateDiff("s", strLastUpdate, Now)) Then
result = true
SetLastUpdateTime
Else
result = false
End If
CacheExpires = result
End Function
Sub SetLastUpdateTime
Application.Lock
Application("LastUpdate") = CStr(now())
Application.UnLock
End Sub
Sub GetHTMLStream
If IsExpires Then
UpdateHTMLStream
End If
HTMLStream=Application("CACHE_HTMLStream")
End Sub
Sub UpdateHTMLStream
dim d
d = FetchHTMLStream
Application.Lock
Application("CACHE_HTMLStream") = d
Application.UnLock
End Sub
Function FetchHTMLStream
Dim rs, strSQL, strHTML
Set rs = CreateObject("ADODB.Recordset")
strSQL = "select categoryID , categoryname from categories"
rs.Open strSQL, strConn,adOpenForwardOnly,adLockReadOnly
strHTML = strHTML & "<select name=""slt_search"">"
while (not rs.EOF)
strHTML = strHTML & "<option>"
strHTML = strHTML & rs.Fields("categoryname")
strHTML = strHTML & "</option>" rs.MoveNext
wend
strHTML = strHTML & "</select>"
rs.Close
Setrs=Nothing
FetchHTMLStream = strHTML
End Function
%>
ASP: conn.asp
<!--METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library" TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}"-->
<%
dim strConn
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind"
%>