[ASP]提高数据显示效率--缓存探幽

作者:5do8 来源:蓝色理想 时间:2008-05-18 13:51:00 

写在前面的话:此篇还是asp相关的,相信玩ASP的都有这个感觉,当数据有5万多条时-------just like音乐网,要调用最新的10条在页面显示,糟糕的是,当n多用户打开页面访问的时候,每个用户每次都要读取数据库一次,这无疑降低了效率,很明显,如果能把数据能保存在内存上,然后读取,无疑加快了速度.

所谓缓存其实就是在内存中开辟一个用来保存数据的空间,使用缓存你就不用频繁的访问你保存在硬盘上的数据了,因为这些数据我们希望每个用户都能看到效果一样,考虑使用的是application对象,因为它是所有访问者的共用的对象,存储的信息和定义的事件能够为所有者访问者使用,这里要使用asp内置对象APPLICATION了,关于application,有2个方法[lock和unlock],2个集合[content和staticobjects],2个事件[开始的application_onstart和application_end],application变量不会因为用户的离开而消失,一旦建立,一直等到网站关闭和程序卸载为止,正因为如此,使用的时候要特别小心!,否则会占用内存,我在这里不用多说,有兴趣的查阅相关资料吧,大体是这样.我们是把数据写入一个自定义的application里面,在制定的时间读取刷新的,大体思路就是这样.

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



说明完毕.

以下为完整无注释代码





<%
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
Response.Write "<b><font color=""green"">利用缓存读取数据"
Response.Write " ... (" & temp1 & " 秒剩余)</font></b>"
Response.Write "<br><br>"
Else
Dim temp2
temp2 = DisplayRecords()
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)%>


写在后面的话:如果你感觉你的服务器内存不够大的话,不要大量使用缓存.

远程下载: cache.rar

本地下载:cache-asp.rar (8.71 KB)

标签:缓存,APPLICATION,数据,asp
0
投稿

猜你喜欢

  • 分享个asp文件缓存代码,使程序从缓存读数据

    2011-03-09 19:47:00
  • 一个20行左右的强$代码

    2009-12-25 15:22:00
  • HTML5 Canvas 起步(2) - 路径

    2009-05-12 12:06:00
  • ASP 字符串转数字格式

    2009-08-19 17:18:00
  • MySQL重定位数据库目录的内容

    2009-02-26 16:03:00
  • 一次性压缩Sqlserver2005中所有库日志的存储过程

    2012-01-29 17:58:28
  • js检查全角字符正则表达式[\\uFE30-\\uFFA0]

    2008-10-30 12:39:00
  • 请谨慎对待程序的图标和名称

    2011-06-16 20:35:22
  • SQL Server 总结复习(一)

    2012-10-07 11:04:02
  • 用javascript获得地址参数的两种方法

    2008-04-30 12:17:00
  • asp如何处理页面执行时发生的错误?

    2009-11-14 20:43:00
  • DW自带的行为制作弹出菜单

    2008-05-16 11:38:00
  • SQL数据库操作类

    2009-01-14 16:26:00
  • 段正淳的css笔记(4)css代码的简写

    2007-11-01 22:03:00
  • Mootools 1.2教程(12)——用Drag.Move实现拖拽和拖放

    2008-12-05 12:29:00
  • 使用SQL Server 2000索引视图提高性能

    2009-01-13 13:47:00
  • FrontPage XP设计教程2——网页的编辑

    2008-10-11 12:16:00
  • 两大步骤教您开启MySQL 数据库远程登陆帐号

    2010-09-30 16:42:00
  • 让ASP也支持动态include文件

    2008-05-08 13:00:00
  • 用asp编写类似搜索引擎功能的代码

    2008-10-23 15:55:00
  • asp之家 网络编程 m.aspxhome.com