The application of cache technology in ASP is simply introduced

Source: Internet
Author: User
Tags end flush include
Cache ASP from the release has been 7 years, the use of ASP technology has been quite mature, since the introduction of Microsoft ASP.net has gradually stopped the ASP version of the update. But because a lot of people are still used to using ASP to develop Web sites, again I will use a simple example to illustrate how cache is used in ASP.

Simply put, the basic principle of using cache is to keep the frequently needed and costly data in memory for a certain amount of time, so that the data can be accessed directly and globally. For example, some data needs to be queried from multiple tables in the database, and almost every page calls that data.

In this case the best implementation is to cache this part of the data, the simple implementation in ASP is the final expression of the data (such as HTML stream) encapsulated in string and then into the ASP built-in object application (this article is mainly about dynamic cache, Simple ASP application is omitted). The advantage of this is that the HTML can be called globally throughout the site, and application is in memory, so no more querying the database, which accelerates response time and saves server load. This is, of course, at the expense of memory, and is a typical example of space-time change.

Although there are many benefits to using this method, this method may no longer be applicable when encountering a frequently changing data source (database), because the ASP Application object has a disadvantage that it cannot automatically change with the data source or control the refresh interval. So developers need to program to implement dynamic cache. Of course, when the program is programmed, you can update the appliction when all the data source (database) operations are changed. This keeps the data source (database) consistent. There are more problems to be considered in programming, and it is easy to omit details. So I do not recommend this method except in certain circumstances.

I think the best way in the ASP is to use the program to achieve a timed flush cache, that is, to set an expiration time stored in the application. Of course, the Application object does not have such a expiretime attribute in the ASP. This needs to be implemented in a program.

Code
ASP:default.asp

<% @Language =vbscript%>
<%option explicit%>
<%Response.Buffer=True%>
<!--#include file = "conn.asp"-->
<!--#include file = "getcache.asp"-->
<HTML>
<HEAD>
<title>asp Cache Demo </TITLE>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</HEAD>
<BODY>
<%
Response. Flush
Gethtmlstream
Response. Write
Htmlstream
%>
</body>

ASP:getcache.asp

<%
Const cache_default_interval = 30 ' refresh CACHE every 30 seconds
Dim Htmlstream
Dim Isexpires
Isexpires = Cacheexpires
Function Cacheexpires
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
Set rs = 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 "
%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.