欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-15 10:51:37
实例演示.先建立一个简单的,写个function读取一下,写入一个dim变量temp中: asp代码 <%      &nbs...

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