Examples of simple data caching principles

Source: Internet
Author: User
Tags object current time end sql access
Let's start by analyzing the code directly. If you do not understand the basic features of the Application object, you can view the object reference section of this site.

<%
The following function is used to present the data update cache from the Dynamic web Forum database. 10 posts in the latest post
function Refreshrecords ()
Dim SQL, Conn, RS
sql = "SELECT top * from Dv_topic ORDER BY dateandtime Desc"
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= "&server.mappath (" Dv.mdb ")
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open Sql,conn, 1, 1

If not Rs. EOF Then
Dim Temp
temp= "<ul>"
For I=1 to 10
temp = temp& "<li><a href=" "Http:*/bbs.sfte.net/dispbbs.asp?
Boardid= "&rs" ("Boardid") & "&id=" &rs
("TopicID") & "&page=1" ">" &rs ("Title") & "(" &rs ("Postusername")
& ") </a></li>"
"This place is tastes, obviously with a table,td,tr,tbody to write the front of the people to cooperate, is a very painful thing."
Rs. MoveNext
I=i+1
Next
Temp = temp & "</ul>"
Refreshrecords = Temp
Else
Refreshrecords = "Data call failed."
End If
Rs. Close
Conn. Close
Set rs = Nothing
Set conn = Nothing
End Function


"The following function is used to update the cached

function Displayrecords (secs)

If Application ("cache") = "" Or IsEmpty (Application ("cache") then "" The program updates the cache the first time it is run
"Application" ("cache") is used to save what we need to cache in memory
' Application (' Cache_time ') is used to preserve the time the cache was last updated
"We only need to call the Refresh function in both cases to access the database to update the cache
The first is a server reboot or other unexpected loss of the value of the application store
Application.Lock
Application ("Cache_date") =now ()
' Update cache time
Application ("cache") = Refreshrecords ()
' Update cached content
"' Codeby Niceidea signature
Application.UnLock
End If

If DateDiff ("s", Application ("Cache_date"), now) > secs Then ' compare the difference between the last update time and the current time
The other thing that needs to be updated is cache expiration. A cache is a regular update of a certain amount of data. I'm afraid it's the most common.
Application.Lock
Application ("Cache_date") =now ()
Application ("cache") = Refreshrecords ()
Application.UnLock
End If
Response.Write Application ("cache")
End Function
%>

The last method called is
<%displayrecords (%>)
300 indicates a 5-minute update 60x5=300!



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.