读书人

JSP高访问量停的计数程序

发布时间: 2011-12-21 23:56:01 作者: rapoo

JSP高访问量下的计数程序
有时要为每一篇文章统计其点击次数,如果每一次浏览都要更新一次库的话,那性能在访问量很大的情况下,服务器的压力就会很大了,比较好一点的方法就是先将要更新的数据缓存起来,然后每隔一段时间再利用数据库的批量处理,批量更新库。源码如下:

  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;

/**
*
* @author http://www.tot.name
*/
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.*;
/**
*
* @author http://www.tot.name
*/
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.*;
/**
*
* @author http://www.tot.name
*/
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> ");
%>


转天极

[解决办法]
好。
[解决办法]
不错
[解决办法]
还是在逼不得已的情况下才在JSP中写代码
[解决办法]
好东西 ,谢谢分享 。
[解决办法]
专业的JSP空间服务商 专业的电信 '3线JSP空间,JSP虚拟主机

我们是专业的JSP空间,不做ASP和PHP,只有专业才能更好,更放心,,,


价格绝对够优惠,欢迎大家和我联系,或者登陆我们的网站:http://www.jsp8.cn

我的QQ: 282773217 我电话:0552-8718771 大家可以和我联系哟,给您最优惠的价格,


还有我们的环境为TOMCAT5.5.12 JDK1.5 MYSQL5.0.27 MSSQL2000


[解决办法]
good~
[解决办法]
使用一些cache就可以了,干吗这么写? oscache,ecache都能完成这么简单的功能
[解决办法]
mark
[解决办法]
好东西,收藏一下。。。
[解决办法]

------解决方案--------------------


学习了
[解决办法]
学习
[解决办法]
所谓的延迟策略
[解决办法]
搞这大堆东西计算时间没觉得会比update一次数据库好多少~
wmzsl(王明哲)好象正解,缓存^_^!

[解决办法]
学习,收藏
[解决办法]
up
[解决办法]
up

读书人网 >Java Web开发

热点推荐