您当前的位置: 首页 > 网站编程 > ASP教程 > ASP实例:利用缓存提高数据显示效率

ASP实例:利用缓存提高数据显示效率

作者:guanchaofeng 来源:不详 发布时间: 2009-03-23 14:15 点击:
实例演示.先建立一个简单的数据库,写个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 "D

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> 

分享到:
本文"ASP实例:利用缓存提高数据显示效率"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4