ASP improves data display efficiency-Cache Exploration

Source: Internet
Author: User

The so-called cache is actually to open up a space in the memory to store data. Using the cache, you don't need to frequently access the data you saved on the hard disk, because of this data, we hope that every user can see a similar effect. We consider using an application object because it is the shared object of all visitors, the stored information and defined events can be used by the visitor of the owner. here we need to use the ASP built-in object application. For application, there are two methods [Lock and unlock]. two sets of [content and staticobjects], two events [Starting application_onstart and application_end], the application changes will not disappear because of the user's departure. Once the application is established, it will wait until the website is closed and Program So far, it is because of this that you should be very careful when using it !, Otherwise, the memory will be occupied. I don't need to talk about it here. I 'd like to check the relevant information. This is generally the case. we write data into a custom application and read and refresh the data at the specified time. The general idea is as follows.

Example: create a simple database, write a function to read it, and write it to the dim variable temp:

The following is a reference clip:
CopyCode The Code is as follows: function displayrecords ()
'The function originally provided the record value to the variable temp.
Dim SQL, Conn, RS
'SQL statement that meets the condition
SQL = "select ID, [szd_f], [szd_t] from admin"
'Open the database connection
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
'When the data that meets the SQ statement l is not displayed
If not Rs. EOF then
'Assign a value to the temp variable
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> operation </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>"
'Temp is assigned, and it is returned to the function.
Displayrecords = temp
Else
Displayrecords = "Data not available ."
End if
'Release memory
Rs. Close
Conn. Close
Set rs = nothing
Set conn = nothing
End Function

OK. After the above function transformation is completed, displayrecords is called.
The following shows how the application works: Copy code Code: 'This function is written to the cache.
Function displaycachedrecords (SECs)
Dim retval, datval, temp1
'Secs indicates the time to refresh the data, retval indicates the data, and datval indicates the remaining time.
Retval = Application ("cache_demo") 'gets the appliction value.
Datval = Application ("cache_demo_date") 'gets the value of appliction.
'Judge the value of datval, that is, whether the calculation time has passed.
If datval = "" then
'If it is null, The datval value is the current time plus the time defined by secs in seconds.
Datval = dateadd ("S", secs, now)
End if
'Temp1 is the second difference between the current time and datval.
Temp1 = datediff ("S", now, datval)
'If retval is already the return value of the above function and the time is greater than 0
If temp1> 0 and retval <> "" then
'The number of records returned by this function
Displaycachedrecords = retval
Response. Write "<B> <font color =" "green"> Use cache to read data"
Response. Write "... (" & temp1 & "seconds remaining) </font> </B>"
Response. Write "<br>"
Else
If 'retval is null, assign the displayrecords value to the variable temp2.
Dim temp2
Temp2 = displayrecords ()
'Save to application. ------------------> important
Application. Lock
Application ("cache_demo") = temp2
Application ("cache_demo_date") = dateadd ("S", secs, now)
Application. Unlock
Displaycachedrecords = temp2
'The past Cache Time of the record is casually written here, which is less than the total number of seconds:
Response. Write "<B> <font color =" red "> refresh cache display ..."
Response. Write "</font> </B> <br>"
End if
End Function
%>

Description completed.

The complete uncommented code is as follows:

Call method: <% = displaycachedrecords (20) %>

If you feel that your server memory is not large enough, do not use the cache in large quantities.

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.