Application of Cache Technology in ASP

Source: Internet
Author: User

ASP has been released for seven years, and the use of ASP technology has been quite mature. Since Microsoft launched ASP. NET, it has gradually stopped updating ASP versions. However, since many people are still used to developing websites using ASP, I will use a simple example to illustrate how to use cache in ASP.
Simply put, the basic principle of using cache is to keep frequently-needed and expensive data in the memory for a certain period of time for direct global access to the data. For example, some data needs to be queried from multiple tables in the database, and almost every page needs to call the data.

In this case, the best implementation is to cache this part of data. In ASP, the simple implementation is the final expression of the data (such as HTML stream) it is encapsulated in the string and then stored in the ASP built-in object application (this article mainly discusses dynamic cache, and simple ASP applications will be omitted ). The advantage of doing so is that the entire website can call this HTML segment globally, and the application is in memory, so you do not need to query the database, which speeds up the response time and saves the server load. Of course, this is at the cost of memory consumption. It is a typical instance that changes the time of space.

Although this method has many advantages, this method may no longer be applicable when you encounter frequently changing data sources (databases), because the ASP application object has a disadvantage, that is, it cannot automatically change with the data source, or control the refresh interval. Therefore, developers need to program to implement dynamic cache. Of course inProgramThe appliction can be updated once during all data source change (database) operations. In this way, the data source (database) is always consistent. In this way, there are many issues to be considered in programming, so it is easy to omit details. Therefore, I do not recommend this method unless otherwise specified.

I think the best way in ASP is to use programming to regularly refresh the cache, that is, to set an expiration time for the storage in the application. Of course, the application object in ASP does not have such an expiretime attribute. This needs to be implemented using 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>
<H4> refresh the cache every 10 seconds: </H4>
<%
Response. Flush
Gethtmlstream
Response. Write
Htmlstream
%>
</Body>
</Html>

ASP: getcache. asp

<%
Const cache_default_interval = 30' refresh the 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"
%>

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.