一个基于XML的新闻点击率收集器

       实现新闻点击率收集器并不难,但制作一个节约系统资源,反应灵敏的的收集者就要我们发挥想象力了。这里向给出一个我想出来的解决方案。一个利用xml文件来实现中间缓存的点击率收集器。

        其开发思想为:

                    浏览者-----------新闻---------xml文件-------------数据库

                                查看             插入记录            当记录数大于一个给定值,向数据库执行批量更新。

 

       这个收集器只是利用了xml文件来暂时缓存数据,达到不频繁调用数据库连接,节约系统资源,使新闻访问速度加快。

 

-------------------------------------------------数据----------------------------------------

首先我们来建张新闻表:infomation

  字段                  数据类型

info_id                  int              标识   主键

info_caption         varchar(100)

info_context          ntext(16)

info_date               varchar(30)

info_count             bigint

 

做缓存用的xml文件 tempcount.xml

<newscount>---根节点

<countsum></countsum>  总点击数,后面要加个逻辑来判断这个数当符合条件时调用批量更新

<note newid="">  newid为新闻标号对应info_id

<tempcount></tempcount> 该新闻点击的数目

</note>

 

</newscount>

 

----------------------------------------------------------

 

 

-------------------------------------------商务逻辑-----------------------------------

我们在项目里建个类文件取名为newscount

下面的介绍该类的几个核心方法:

首先是addNewCount()方法,该方法是当用户点击新闻时调用的,收集用户点击新闻的信息,并把该信息插入到xml文件中

  1.  public static void addNewCount(int newid)
  2.     {
  3.         string tempfile = HttpContext.Current.Server.MapPath(@"~/fore/tempcount.xml");
  4.         bool flag = false;//用来标识消息队列中的缓存的消息是否超过范围
  5.         XmlDocument document = new XmlDocument();
  6.         document.Load(tempfile);
  7.         XmlNode tmpNode = document.SelectSingleNode("newscount/countsum");
  8.         int intTemp = int.Parse(tmpNode.InnerText);
  9.        
  10.         intTemp  += 1;
  11.         if (intTemp >= int.Parse(ConfigurationManager.AppSettings["CountQueue"].ToString())) flag = true;//判断是否需要调用方法uptCountQueue()
  12.         tmpNode.InnerText = intTemp.ToString();
  13.         XmlNode tmpNewNode = document.SelectSingleNode("newscount/note[@newid=" + newid.ToString() + "]");//xpath 返回属性newid为指定内容的note节点
  14.         if (tmpNewNode != null)
  15.         {
  16.           
  17.             intTemp = int.Parse(tmpNewNode.SelectSingleNode("tempcount").InnerText);
  18.             intTemp += 1;
  19.             tmpNewNode.SelectSingleNode("tempcount").InnerText = intTemp.ToString();
  20.         }
  21.         else
  22.         {
  23.             //新建一个note节点来保存点击信息
  24.             XmlNode root = document.DocumentElement;
  25.             XmlElement child = document.CreateElement("note");
  26.             XmlAttribute attribute = document.CreateAttribute("newid");
  27.             attribute.Value=newid.ToString();
  28.             child.Attributes.Append(attribute);
  29.             XmlElement child2;
  30.             child2 = document.CreateElement("tempcount");
  31.             child2.InnerText ="1";
  32.             child.AppendChild(child2);
  33.             root.AppendChild(child);
  34.         }
  35.         document.Save(tempfile);
  36.         if (flag) uptCountQueue();//如果符合条件就调用更新方法
  37.     }

     接下来要介绍的是更新方法uptCountQueue(),该方法主要作用为读取xml文件tempcount.xml,分析里面的数据,然后将收集

到的数据更新到数据库中。

 

 

  1.  public static void uptCountQueue()
  2.     {   
  3.         string tmpFilePath = HttpContext.Current.Server.MapPath(@"~/fore/tempcount.xml");
  4.         XmlReader reader = XmlReader.Create(tmpFilePath);   //xmlReader
  5.         DataTable updTable = new DataTable();
  6.         DataColumn column = new DataColumn("info_id"typeof(int));
  7.         updTable.Columns.Add(column);
  8.         column = new DataColumn("info_count"typeof(Int64));
  9.         updTable.Columns.Add(column);
  10.         DataRow row;
  11.        while (reader.Read())
  12.         {
  13.             if (reader.NodeType == XmlNodeType.Element && reader.Name == "note")
  14.             {
  15.                row=updTable.NewRow();
  16.                XmlReader readerSubTree = reader.ReadSubtree();
  17.                 while (readerSubTree.Read())
  18.                 {
  19.                     if (readerSubTree.NodeType == XmlNodeType.Element && readerSubTree.Name == "note")
  20.                     {
  21.                         if (readerSubTree.MoveToAttribute("newid"))  //判断节点note是否包含属性newid,如果存在将移动到该属性
  22.                         row[0] = readerSubTree.Value;    //读取该属性的值
  23.                        
  24.                     }
  25.                     if (readerSubTree.NodeType == XmlNodeType.Element && readerSubTree.Name =="tempcount")
  26.                     {
  27.                         row[1] = reader.ReadElementContentAsInt();
  28.                     }
  29.                 }
  30.                 
  31.                
  32.                 updTable.Rows.Add(row);
  33.             }
  34.         }
  35.         reader.Close();
  36.         string filter=null;  //用来设置下面sql过滤条件
  37.         foreach (DataRow r in updTable.Rows)
  38.             filter += r[0].ToString() + ",";
  39.         filter=filter.Substring(0, filter.Length - 1);//去处多余的“,”
  40.         DataSet ds = new DataSet();
  41.         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["school_river"].ToString());
  42.         SqlCommand cmd = new SqlCommand("select info_id,info_count from infomation where info_id in ("+filter+")", conn);
  43.         SqlCommand cmd2=new SqlCommand("update infomation set info_count=info_count+@info_count where info_id=@info_id",conn);
  44.         
  45.         
  46.        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
  47.        adpt.UpdateCommand=cmd2;
  48.        ad pt.UpdateCommand.Parameter.add(New SqlParameter("@info_count",SqlDbType.BigInt,8,"info_count"));
  49.        adpt.UpdateCommand.Parameter.add(New SqlParameter("@info_id",SqlDbType.Int,4,"info_id"));
  50.        // SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adpt);  //SqlCommandBuilder
  51.         adpt.Fill(ds);
  52.         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)           //修改获取的数据
  53.             ds.Tables[0].Rows[i][1] = updTable.Rows[i][1];
  54.          adpt.Update(ds.Tables[0]);                 //更新
  55.         XmlDocument doc = new XmlDocument();
  56.         doc.LoadXml(@"<newscount><countsum>0</countsum></newscount>");           //重构tempcount.xml文件
  57.         doc.Save(HttpContext.Current.Server.MapPath(@"~/fore/tempcount.xml"));
  58.         
  59.         
  60.         
  61.     
  62.     }

到这里这个点击率收集者的,主要框架也算打好了,还有就是在web.config文件里再添加写内容:

<appSettings>

<add key="CountQueue" value="50"></add>

</appSettings>

里面的value存储的值为tempcount.xml中可以保存的点击数,如果大于该数的话,系统将自动调用uptCountQueue()方法。

 

使用这个收集者个时候只要把方法addNewCount()添加到新闻显示页面的Page_load事件中就可以了.

 

该收集者已经打包了,下载地址为:http://download.csdn.net/source/697297

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值