用户名: 密码: 验证码:           网站地图 高级搜索 RSS订阅 收藏本站
Google
您的位置:首页>>网络编程>>ASP编程>>阅读资讯:ASP实例:利用缓存提高数据显示效率

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

[ 来源: | 阅读:次 | 更新日期:2007-9-29 00:17:44 | 评论 0 条 | 我要投稿 ]

实例演示.先建立一个简单的数据库,写个function读取一下,写入一个dim变量temp中: yueluo.net

ASP代码

字串6

<%       
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"""      

yueluo.net


      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")        字串9
      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       字串5
    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>        字串9
<%       
    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> 
字串7


Tags:ASP,缓存,数据显示
责任编辑:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为