实例演示.先建立一个简单的数据库,写个function读取一下,写入一个dim变量temp中:
以下是引用片段:
复制代码 代码如下:
function displayrecords()
'这个函数原来给一个变量temp付上记录的值
dim sql, conn, rs
'符合条件的sql语句
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
'当符合sq语句l的数据没有显示完毕时
if not rs.eof then
'给temp变量赋值
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>"
'temp赋值完毕,把它再返回给函数
displayrecords = temp
else
displayrecords = "data not available."
end if
'释放内存
rs.close
conn.close
set rs = nothing
set conn = nothing
end function
ok,上面的函数改造完毕,调用的时候就是displayrecords.
下面是application大显身手了:
复制代码 代码如下:
'该函数是写入缓存
function displaycachedrecords(secs)
dim retval, datval, temp1
'secs是每次要刷新数据的时间, retval是数据,datval是剩余时间
retval = application("cache_demo") '取得appliction的值
datval = application("cache_demo_date") '取得appliction的值
'判断datval 的值,也就是要计算时间过去了没
if datval = "" then
'如果是空,datval值为当前时间按秒加上secs定义的时间
datval = dateadd("s",secs,now)
end if
'temp1是判断当前时间和datval的秒差
temp1 = datediff("s", now, datval)
'如果retval已经是上面函数的返回值且时间大于0
if temp1 > 0 and retval <> "" then
'本函数返回记录数
displaycachedrecords = retval
response.write "<b><font color=""green"">利用缓存读取数据"
response.write " ... (" & temp1 & " 秒剩余)</font></b>"
response.write "<br><br>"
else
'retval 是空的话,就赋予displayrecords的值给变量temp2
dim temp2
temp2 = displayrecords()
'保存到application.------------------>重点
application.lock
application("cache_demo") = temp2
application("cache_demo_date") = dateadd("s",secs,now)
application.unlock
displaycachedrecords = temp2
' 这里随便写上了记录的缓存的过去时间,相对总秒数倒差 :
response.write "<b><font color=""red"">刷新缓存显示 ..."
response.write "</font></b><br><br>"
end if
end function
%>
说明完毕.
以下为完整无注释代码
调用方法:<%=displaycachedrecords(20)%>
写在后面的话:如果你感觉你的服务器内存不够大的话,不要大量使用缓存.
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!