ASP instance: Improve data display efficiency with caching

Source: Internet
Author: User

Example Demo: First create a simple database, write a function read it, write a Dim variable Temp:

ASP code

The following are the referenced contents:
<%
Function Displayrecords ()
Dim SQL, Conn, RS
sql = "SELECT ID, [Szd_f], [szd_t] from admin"
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= "&server.mappath (" Db.mdb ")
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open SQL, conn, 1, 3
If not Rs. EOF Then
Dim Temp
temp = "<table width=" "90%" "align=" "Center" "
Temp = temp & "border=" "1" "bordercolor=" "Silver" ""
Temp = temp & "cellspacing=" "2" "cellpadding=" "0" ">"
Temp = temp & "<tr bgcolor=" "#CCDDEE" "><td width=" "5%" ""
Temp = temp & ">ID</td><td> operations </td>"
Temp = temp & "<td> value </td></tr>"
While not Rs. Eof
Temp = temp & "<tr><td bgcolor=" "#CCDDEE" ">"
Temp = temp & RS ("ID") & "</td><td>" & RS ("Szd_f")
Temp = temp & "</td><td>" & RS ("szd_t")
Temp = temp & "</td></tr>"
Rs. MoveNext
Wend
Temp = temp & "</table>"
Displayrecords = Temp
Else
Displayrecords = "Data not Available."
End If
Rs. Close
Conn. Close
Set rs = Nothing
Set conn = Nothing
End Function
' Write Cache
Function displaycachedrecords (secs)
Dim RetVal, Datval, Temp1
RetVal = Application ("Cache_demo")
Datval = Application ("Cache_demo_date")
If datval = "" Then
Datval = DATEADD ("s", Secs,now)
End If
Temp1 = DateDiff ("s", now, Datval)
If temp1 > 0 and RetVal <> "" Then
Displaycachedrecords = RetVal
' Debugging Code:
Response.Write "<b><font color=" "Green" > read data using cache
Response.Write "... ("& Temp1 & seconds remaining) </font></b>"
Response.Write "<br><br>"
Else
Dim Temp2
' Change Displayrecords () to the function whose
' Value you want to cache
Temp2 = Displayrecords ()
Application.Lock
Application ("Cache_demo") = Temp2
Application ("cache_demo_date") = DateAdd ("s", Secs,now)
Application.UnLock
Displaycachedrecords = Temp2
' Debugging Code:
Response.Write "<b><font color=" "Red" > Refresh cache Display ... "
Response.Write "</font></b><br><br>"
End If
End Function
%>
<!--
Response.Write Displayrecords ()
-->
<title> read data from database---using caching </title>
<style>
Body, p, TD {Font-family:sans-serif; font-size:8pt;}
td {Padding-left:5;}
</style>
<body>
<%
Dim T1, T2
T1 = Timer
Response.Write Displaycachedrecords (20)
T2 = Timer
%>
<p align= "center" >
Stay time: <%= Left ((CDBL (T2-T1) * 1000.0), 5)%> ms
</p>
</body>



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.