学院首页>网络编程>ASP>用一个“静态”的ASP页面来改进你的服务器的性能

用一个“静态”的ASP页面来改进你的服务器的性能

作者:未知 来源:chinaasp 添加时间:2006-5-21 10:28:39
通常大家显示一个数据库中的信息时都是使用动态页面来生成的,

这对于一个小网站或者当数据库内的容量不大时,系统的性能并没有什么影响。

但是当用户要频繁地访问一个数据量很大的库时,系统是不是还能够承受得了了。

下面介绍一种“静态”ASP技术来解决这个问题。

例如现在这个有一个人员资料库,结构如下:

ID First  LastCompanyEmail Phone

常规的办法如下:

contact.asp

<table cellspacing=0 cellpadding=0>

<%

set query = getdb.execute("select * from contacts order by firstname, lastname")

do while not query.eof



response.write "<tr><td><a href="""

response.write "detail.asp?id=" & query("id")

response.write """>" & query("first") & " " & query("last")

response.write "</a></td></tr>"

query.movenext



loop

query.close

set query = nothing

%>

</table>



detail.asp

<table cellspacing=0 cellpadding=0>

<%

set query = getdb.e xecute("select * from contacts where id=" & request("id"))

if not query.eof then



response.write "<tr>"

response.write "<td>Name: </td><td>" & query("first") & " " & query("last") & "</td>"

response.write "<td>Company: </td><td>" & query("company") & "</td>"

response.write "<td>E-mail: </td><td>" & query("email") & "</td>"

response.write "<td>Phone: </td><td>" & query("phone") & "</td>"

response.write "</tr>"



query.movenext



end if

query.close

set query = nothing

%>

</table>



我想大家对上面的代码应该是不会有什么疑问的,显然它存在我上面提出的那个问题。

就是当每次显示一个人的详细资料时,都会读取数据库。

现在我提出的这个想法其实很简单,就是使用一个“静态”的ASP页面来代替读取数据库

的操作。

调用格式如下:

"Contact" & ID & ".asp"

例如我想读取id为27的人的信息,我就不用去查取数据库了,只要显示一个静态的

"Contact27.asp"就可以了。

这时你也许会说,如果我要改变了一个人的信息怎么办,其实只要在将

用户信息保存后的同时也改写这个静态页面,代码如下,有没有兴趣研究研究呀。

sub GenerateContactCacheFile(id)

dim query

set query = getdb.execute("select * from contacts where id=" & id)

if not query.eof then

 dim filename

filename = "Contact" & id & ".asp"

dim fso

set fso = server.createobject("scripting.filesystemobject")

dim file

set file = fso.createtextfile(filename)

file.writeline "<html><head>"

file.writeline "<title>Contact: " & query("first") & " " & query("last") & "</title>"

file.writeline "</head><body>"

file.writeline "<table cellspacing=0 cellpadding=0>"

file.writeline "<tr>"

file.writeline "<td>Name: </td><td>" & query("first") & " " & query("last") & "</td>"

file.writeline "<td>Company: </td><td>" & query("company") & "</td>"

file.writeline "<td>E-mail: </td><td>" & query("email") & "</td>"

file.writeline "<td>Phone: </td><td>" & query("phone") & "</td>"

 file.writeline "</tr>"

file.writeline "</table>"

file.writeline "</body></html>"

file.close

set file = nothing

end if

query.close

set query = nothing



end sub

使用模版,很多人在编程的时候都喜欢使用模版文件,我也很喜欢这样

因为这样能够让整个网站的风格保持一致,同时还可以免去讨厌的frame

一个典型的使用模版文件的代码如下:

<html>

<head>

<title>我的主页</title>

<!-- #include file="style.inc" -->

</head>

<body>

<!-- #include file="navstart.inc" -->

<!-- #include file="adbox.inc" -->



 ......这一页的内容.....



<!-- #include file="adbox.inc" -->

<!-- #include file="navend.inc" -->

</body>

</html>



这样,当你有多个“静态”页面时,尤其是上万个页面时,可以使用下面这种方式:

sub GenerateHeader(file, title)

file.writeline "<html><head>"

file.writeline "<title>" & title & "</title>"

file.writeline "<!-- #include file="style.inc" -->"

file.writeline "</head></body><body>"

file.writeline "<!-- #include file="navstart.inc" -->"

file.writeline "<!-- #include file="adbox.inc" -->"

end sub



sub GenerateFooter(file)

file.writeline "<!-- #include file="adbox.inc" -->"

file.writeline "<!-- #include file="navend.inc" -->"

file.writeline "</body></html>"

end sub





GenerateHeader file, "Contact: " & query("first") & " " & query("last")

file.writeline "<table cellspacing=0 cellpadding=0>"

file.writeline "<tr>"

file.writeline "<td>Name: </td><td>" & query("first") & " " & query("last") & "</td>"

file.writeline "<td>Company: </td><td>" & query("company") & "</td>"

file.writeline "<td>E-mail: </td><td>" & query("email") & "</td>"

file.writeline "<td>Phone: </td><td>" & query("phone") & "</td>"

file.writeline "</tr>"

file.writeline "</table>"

GenerateFooter file





最后是在contacts.asp中把从数据库中读数据改成读“静态页面”就可以了。

response.write "<tr><td><a href="""

response.write "contact" & query("id") & ".asp"' point the link to the cache page...

response.write """>" & query("first") & " " & query("last")

response.write "</a></td></tr>" 

试试把,这个方法能够把你服务器的性能大大提高的哦。
站内搜索