有時要為每一篇文章統計其點擊次數,如果每一次瀏覽都要更新一次庫的話,那性能在訪問量很大的情況下,服務器的壓力就會很大了,比較好一點的方法就是先將要更新的數據緩存起來,然后每隔一段時間再利用數據庫的批量處理,批量更新庫。源碼如下:
countbean.java
/*
* countdata.java
*
* created on 2006年10月18日, 下午4:44
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/
package com.tot.count;
/**
*
*/
public class countbean {
private string counttype;
int countid;
/** creates a new instance of countdata */
public countbean() {}
public void setcounttype(string counttypes){
this.counttype=counttypes;
}
public void setcountid(int countids){
this.countid=countids;
}
public string getcounttype(){
return counttype;
}
public int getcountid(){
return countid;
}
}
countcache.java
/*
* countcache.java
*
* created on 2006年10月18日, 下午5:01
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/
package com.tot.count;
import java.util.*;
/**
*
*/
public class countcache {
public static linkedlist list=new linkedlist();
/** creates a new instance of countcache */
public countcache() {}
public static void add(countbean cb){
if(cb!=null){
list.add(cb);
}
}
}
countcontrol.java
/*
* countthread.java
*
* created on 2006年10月18日, 下午4:57
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/
package com.tot.count;
import tot.db.dbutils;
import java.sql.*;
/**
*/
public class countcontrol{
private static long lastexecutetime=0;//上次更新時間
private static long executesep=60000;//定義更新間隔時間,單位毫秒
/** creates a new instance of countthread */
public countcontrol() {}
public synchronized void executeupdate(){
connection conn=null;
preparedstatement ps=null;
try{
conn = dbutils.getconnection();
conn.setautocommit(false);
ps=conn.preparestatement("update t_news set hits=hits+1 where id=?");
for(int i=0;i<countcache.list.size();i++){
countbean cb=(countbean)countcache.list.getfirst();
countcache.list.removefirst();
ps.setint(1, cb.getcountid());
ps.executeupdate();⑴
//ps.addbatch();⑵
}
//int [] counts = ps.executebatch();⑶
conn.commit();
}catch(exception e){
e.printstacktrace();
} finally{
try{
if(ps!=null) {
ps.clearparameters();
ps.close();
ps=null;
}
}catch(sqlexception e){}
dbutils.closeconnection(conn);
}
}
public long getlast(){
return lastexecutetime;
}
public void run(){
long now = system.currenttimemillis();
if ((now - lastexecutetime) > executesep) {
//system.out.print("lastexecutetime:"+lastexecutetime);
//system.out.print(" now:"+now+"/n");
// system.out.print(" sep="+(now - lastexecutetime)+"/n");
lastexecutetime=now;
executeupdate();
}
else{
//system.out.print("wait for "+(now - lastexecutetime)+" seconds:"+"/n");
}
}
}
//注:如果你的數據庫驅動支持批處理,那么可以將⑵,⑶標記的代碼前的注釋去掉,同時在代碼⑴前加上注釋
類寫好了,下面是在jsp中如下調用。
<%
countbean cb=new countbean();
cb.setcountid(integer.parseint(request.getparameter("cid")));
countcache.add(cb);
out.print(countcache.list.size()+"<br>");
countcontrol c=new countcontrol();
c.run();
out.print(countcache.list.size()+"<br>");
%>
新聞熱點
疑難解答