對<<在ASP中改善動態分頁的性能>>的不足與修正建議 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 對<<在ASP中改善動態分頁的性能>>的不足與修正建議 (http://www.webasp.net/article/4/3061.htm) |
| -- 作者:未知 -- 發佈日期: 2003-07-11 |
| 對<<在ASP中改善動態分頁的性能>>的不足與修正建議 呵呵,可以進精華區嗎?用了我一天的時間。 兩位張兄的<<在ASP中改善動態分頁的性能>>一文給我很大的啟發,在此表示感謝,但在我想來還是有一些不足的地方。 一是無法反應一些隨更新的信息。 加設現為一BBS,那麼隨時更新的信息有 每個貼子的點擊 新加貼子的信息,這些在兩位張兄兄的方案中無法自動更新,只有用戶在更改查詢時才能使用。 二是內存使用太多,事實上對於一個論壇來講,大多數人還是按順序來訪問的,沒有必要每個人一個session。 三是處理數據時不方便 只能用數組的方式來進行,不直觀,可讀性差。 對於以上幾點,我提出如下改進方案 一 使用application, 可做到多人共同使用同一數據 二 只存入和取出ID號,其它數據做第二次select 三 自動刪除過時的application 以節約內存. 四 在添加和刪除數據時,重新導入數據 其它好處 呵呵,可以使用getstring()了,原來分頁時不行的 可以不用1,3了, 代碼如下 a_page.asp -----------------------------------------------------------------' <% dim apage_pagesize '每頁記錄數 dim apage_Count '總計記錄數 dim apage_PageCount '總頁數 dim apage_PageForm '跳頁用的Form dim apage_PageUrl '上一頁下一頁的鏈接 dim apage_timeout '過期時間設置(秒) apage_timeout=300 '過期時間設置(秒) apage_pagesize=20 ' function apage_bactive (str_name) '判斷是否有這個對象,並刪除過期對像 apage_bactive=false dim item, s_temp for item=1 to Application.contents.count '找出所有的Application s_temp=Application.contents(item) if isarray (s_temp) then if ubound(s_temp)=3 and s_temp(0)="apage" then if s_temp(1)=str_name then apage_bactive=true '要求對像存在 else if DateDiff("s",s_temp(2),now())>apage_timeout then '刪除過其對像 Application.contents.remove(item) end if end if end if end if next end function 'b_inuser sub apage_open (str_name , str_table, str_id, str_sqlend) '打開對象並計算一些數據 '對像名, 表名, 關鍵字名, 查詢條件 dim a1, a_ob if not apage_bactive (str_name) then '如對像不存在則新建一對像 apage_load str_name, str_table, str_id, str_sqlend end if a1=application (str_name) a1(2)=now() application.lock application (str_name)=a1 application.unlock a_ob=a1(3) apage_Count=ubound(a_ob,2)+1 apage_PageCount=int(apage_Count/apage_pagesize)+1 end sub'apage_open function apage_get (str_name, page) '得到本頁的所有id號 dim a1, a_ob, i_stat, i_end, i1, str_actionurl , str_query , str_1, str_2 '以下為保證page是正確的 page=cint (page) if page <1 then page=1 if page >apage_PageCount then page=apage_PageCount '得到id號 apage_get="" a1=application (str_name) a_ob=a1(3) i_stat=(page-1)* apage_pagesize i_end=page* apage_pagesize-1 if i_end>(apage_Count-1) then i_end=apage_Count-1 end if for i1=i_stat to i_end apage_get=apage_get & a_ob(0,i1) & "," next '去掉多餘的","號 if len(apage_get) > 0 then apage_get=left (apage_get, len(apage_get)-1) end if '以下為得到用戶換頁信息 str_actionurl = "http://" & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("SCRIPT_NAME") str_query=Request.ServerVariables("QUERY_STRING") str_1=split (str_query, "&") str_query="" for i1=0 to ubound (str_1) if left (str_1(i1), 5) <> "page=" then response.write left (str_1(i1), 5) & "<br>" str_query=str_query&str_1(i1) & "&" end if next str_2=str_actionurl & "?"&str_query if page>1 then apage_PageUrl="<a href=" &str_2 & "page=1" &">首頁</a> " & _ "<a href="&str_2 & "page="&(page-1) &">前頁</a> " else apage_PageUrl="首頁 前頁" end if if page<apage_PageCount then apage_PageUrl=apage_PageUrl& "<a href="&str_2 & "page="&(page+1) &">後頁</a> " & _ "<a href="&str_2 & "page="&apage_PageCount&">尾頁</a>" else apage_PageUrl=apage_PageUrl& "後頁 尾頁" end if apage_PageForm="<table><form method=get name='page' onsubmit='document.location =""" & str_2 &"page=""+this.page.value;return false;'>"&_ "<tr><td>轉到第<INPUT TYPE='text' NAME='page' value='"&page&"'>頁"&_ "<INPUT type=submit style='font-size: 9pt' value=GO></table>" end function sub apage_load (str_name, str_table, str_id, str_sqlend) '新建或重新導入一對像 sql="select " & str_id & " from " & str_table & str_sqlend set rs=conn.execute (sql) dim a2 (3) a2(0)="apage" a2(1)=str_name a2(2)=now() a2(3)=rs.getrows() application.lock application (str_name)=a2 application.unlock end sub'apage_load sub apage_update(str_name , str_table, str_id, str_sqlend) '更新數據時使用 if apage_bactive (str_name) then '如對像存在則重新導入對像 apage_load str_name, str_table, str_id, str_sqlend end if end sub %> 附:test.asp ---------------------------------------------------------------------- <!--#include file="conn.asp" --> <!--#include file="a_page.asp" --> <% '建表 'create table page '(page_id INT not null IDENTITY (1, 1), 'page_value int not null, 'class_id int not null ') sub add_test '加入測試用數據 dim i1'as int for i1=0 to 1000 sql="insert into page (page_value, class_id) values (" & i1 &",1)" conn.execute sql next for i1=0 to 1000 sql="insert into page (page_value, class_id) values (" & i1 &",2)" conn.execute sql next apage_update "test", "page" , "page_id" , "" 'apage_update "test1", "page" , "page_id" , " where class_id=1" end sub 'add_test 'add_test '要加入時去掉' dim str_test apage_open "test", "page" , "page_id" , "" 'apage_open "test1", "page" , "page_id" , " where class_id=1" str_test =apage_get ("test" ,request("page")) 'str_test= apage_get ("test1" ,request("page")) sql="select * from page where page_id in(" & str_test & ")" set rs=conn.execute (sql) response.write response.write "<table border=1><tr><td>" & rs.getstring (2,,"<td>", "<tr><td>") & "</table>" '或可 'while not rs.eof ' response.write "<a href=hahafish.asp?id=" & rs("page_id") & ">" & rs("page_value") & "</a><br>" 'rs.movenext 'wend response.write apage_PageUrl response.write apage_pageform %> |
| webasp.net |