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

ASP
ASP连接SQL2005数据库连接代码
ASP程序与SQL存储过程结合使用详解
asp 小偷采集程序原理与常用函数方法
防盗链接ASP函数
asp将table生成excel文件(xls)
asp实现新评论自动发短信提示的代码
asp最简单的生成验证码代码
ASP 常见对象总结(熟悉一下利用以后的开发使用)
ASP UTF-8编码生成静态网页的函数
ASP+FSO生成的网页文件默认编码格式以及转换成UTF-8编码方法
asp Access数据备份,还原,压缩类代码
asp fso操作类
ASP 自动采集实现代码
asp 一些支付接口
ASP 递归调用 已知节点查找根节点的函数
用asp实现读取文件的最后一行的代码
用asp实现的获取文件夹中文件的个数的代码
ASP与Excel结合生成数据表和Chart图的代码
iis7 ASP+Access数据库连接错误
ASP 日期的加减运算实现代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 51 ::
收藏到网摘: n/a

实例演示:先建立一个简单的数据库,写个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>