ASP实例:利用缓存提高数据显示效率
生活随笔
收集整理的這篇文章主要介紹了
ASP实例:利用缓存提高数据显示效率
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實例演示:先建立一個簡單的數據庫,寫個function讀取一下,寫入一個dim變量temp中:
ASP代碼
| 以下為引用的內容: <%??????? 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>操作</td>" ????? temp = temp & "<td>數值</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 '寫入緩存??????? 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"">利用緩存讀取數據"?????? ??????? Response.Write " ... (" & temp1 & " 秒剩余)</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"">刷新緩存顯示 ..."?????? ??????? Response.Write "</font></b><br><br>" ??? End If?????? End Function?????? %>??????? <!--??????? Response.Write DisplayRecords()??????? --> <html>??????? <head>??????? ??? <title>利用緩存從數據庫---讀取數據</title>??????? ??? <style>??????? ??? body, p, td { font-family:Sans-Serif; font-size:8pt; }??????? ??? td { padding-left: 5; }??????? ??? </style>??????? </head>??????? <body>??????? <%??????? ??? Dim t1, t2??????? ??????? t1 = Timer??????? ??? Response.Write DisplayCachedRecords(20)??????? ??????? t2 = Timer??????? %> <p align="center">??????? 停留時間: <%= Left((CDbl((t2 - t1) * 1000.0)), 5) %> ms??????? </p> </body>??????? </html>? |
轉載于:https://www.cnblogs.com/wzglweb/archive/2009/02/08/1386237.html
總結
以上是生活随笔為你收集整理的ASP实例:利用缓存提高数据显示效率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用串口电缆实现双机互联(图)
- 下一篇: 聚集索引和非聚集索引[转]