asp+MsSQL2000模拟Html静态文件缓存
这是 JavaScript 写的asp, 看不懂的完了.
其实我根本不想发出来的,因为夸我的人太少了.
如果你以后还想来点经典的怪异思维代码,来,夸我两句.
此代码目的:
不用asp创建Html文件的缓存机智来节约服务器消耗,
直接使用asp来模拟Html的缓存机智,让代码更简单.
好处:
1.比单纯的asp文件优秀得多,开启了缓存机智,不需要任何次都查询多次数据库.
2.而且更改代码更容易,Html静态改次动态代码就得重新生存所有文件.
坏处:
1.相对比Html静态文件来说,他多了模拟缓存的步骤
但是Html静态实际也是由IIS去执行缓存判断,但是ASP执行效率太慢,所以这变成了坏处.如果ASPX的话,这点可以忽视
2.每次模拟缓存时,都会去查询一次asp文件是否发生更改,这里会有一次IIS的查询,还有一次组件的查询,比默认asp多了一次查询.
而静态文件只会查询一次静态文件是否发生更改.
3.每次模拟查询都会去数据库获得一次TimesTamp数据.
修改代码方式:
1.第八行 修改数据连接.
2.在任意数据库中增加数据库字段名称为TimesTamp数据库字段类型为TimesTamp的字段
3.修改38.82行数据库连接方式.
<%@LANGUAGE="JAVASCRIPT"%>
<%
var Conn = {}
Conn.Id = function(DaData){
var Result = + ( ( DaData + "" ) || 0 )
return ( Result > 0 ) ? Result : 0
}
Conn.ConnText = "driver={SQL Server}; server=127.0.0.1; uid=sa; pwd=; database=DADA"
Conn.Create = function(){
try {
Conn.Object = new ActiveXObject("ADODB.Connection");
} catch(Error) {
Conn.Object = null
Response.Write("无法创建数据库对象。" + Error )
Response.End()
}
try {
Conn.Object.Open(Conn.ConnText)
} catch(Error) {
Conn.Object = null
Response.Write("无法打开数据库。" + Error )
Response.End()
}
}
Conn.Create()
//获取asp文件的最后修改豪秒
var HaFso = new ActiveXObject("Scripting.FileSystemObject");
var HaFile = HaFso.GetFile(Server.MapPath(Request.ServerVariables("SCRIPT_NAME")));
var HaAspModifiedTime = new Date(HaFile.DateLastModified).getTime()
var HaFile = null
var HaFso = null
//检测数据库是否发生变化
var HaRs = Conn.Object.Execute("SELECT CAST([TimesTamp] AS int) AS [TimesTamp] FROM [User] WHERE ID = '" + Conn.Id(Request("Id")) + "'")
//最多15个9可转换为日期,但是TimesTamp最长为21个数字,18446744073709551615
var HaTimesTamp = +HaRs.Fields("TimesTamp") + HaAspModifiedTime
HaTimesTamp = parseInt( ( HaTimesTamp / 1000000000000 - (HaTimesTamp / 1000000000000 >> 0) ) * 1000000000000000 )
HaRs.Close()
var HaIfModifiedSince = new Date(HaTimesTamp).toUTCString()
var HaIfNoneMatch = "000000000000000000".substr(HaTimesTamp.toString(36).length) + HaTimesTamp.toString(36).toUpperCase()
var HaHTTP_IF_MODIFIED_SINCE = new Date(Request.ServerVariables("HTTP_IF_MODIFIED_SINCE")).toUTCString()
var HaHTTP_IF_NONE_MATCH = ( Request.ServerVariables("HTTP_IF_NONE_MATCH") + "" ).toUpperCase()
if ( HaIfModifiedSince == HaHTTP_IF_MODIFIED_SINCE ) {
Response.Status = 304
Response.AddHeader("ETag", HaIfNoneMatch)
Response.End()
}else{
Response.Status = 200
Response.AddHeader("Last-Modified", HaIfModifiedSince)
Response.AddHeader("ETag", HaIfNoneMatch)
}
//测试
if ( false ) {
Response.Write( new Date().getTime() + "<br>" )
Response.Write( "If-Modified-Since : " + HaIfModifiedSince + "<br>" )
Response.Write( "ETag : " + HaIfNoneMatch + "<br>" )
Response.Write( "HTTP_IF_MODIFIED_SINCE : " + HaHTTP_IF_MODIFIED_SINCE + "<br>" )
Response.Write( "HTTP_IF_NONE_MATCH : " + HaHTTP_IF_NONE_MATCH + "<br>" )
}
//这里以下是显示内容的代码
Application("Number") = ( Application("Number") || 0 ) + 1
Response.Write( "SQL查询次:" + Application("Number") + "<br>" )
var HaDate = new Date()
Response.Write(
"当前数据缓存日期:"
+ HaDate.getFullYear() + "-" + HaDate.getMonth() + "-" + HaDate.getDay() + "-" + HaDate.getDate()
+ " "
+ HaDate.getHours() + ":" + HaDate.getMinutes() + ":" + HaDate.getSeconds() + ":" + HaDate.getMilliseconds()
)
var HaRs = Conn.Object.Execute("SELECT * FROM [User] WHERE ID = '" + Conn.Id(Request("Id")) + "'")
Response.Write("<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" bordercolor=\"#CCCCCC\">")
Response.Write("<tr>")
for ( var i = 0; i < HaRs.Fields.Count ; i++ ) {
Response.Write( "<td>" + HaRs.Fields(i).Name + "</td>" )
}
Response.Write("</tr>")
Response.Write("<tr>")
for ( var i = 0; i < HaRs.Fields.Count ; i++ ) {
Response.Write( "<td>" + (HaRs.Fields(i).Value || " ") + "</td>" )
}
Response.Write("</tr>")
Response.Write("</table>")
HaRs.Close()
%>