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

本文介绍了一种使用XML文件作为中间缓存的新闻点击率收集器设计方案,旨在减少数据库调用频率,提高系统效率。通过在用户点击新闻时更新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. publicstaticvoidaddNewCount(intnewid)
  2. {
  3. stringtempfile=HttpContext.Current.Server.MapPath(@"~/fore/tempcount.xml");
  4. boolflag=false;//用来标识消息队列中的缓存的消息是否超过范围
  5. XmlDocumentdocument=newXmlDocument();
  6. document.Load(tempfile);
  7. XmlNodetmpNode=document.SelectSingleNode("newscount/countsum");
  8. intintTemp=int.Parse(tmpNode.InnerText);
  9. intTemp+=1;
  10. if(intTemp>=int.Parse(ConfigurationManager.AppSettings["CountQueue"].ToString()))flag=true;//判断是否需要调用方法uptCountQueue()
  11. tmpNode.InnerText=intTemp.ToString();
  12. XmlNodetmpNewNode=document.SelectSingleNode("newscount/note[@newid="+newid.ToString()+"]");//xpath返回属性newid为指定内容的note节点
  13. if(tmpNewNode!=null)
  14. {
  15. intTemp=int.Parse(tmpNewNode.SelectSingleNode("tempcount").InnerText);
  16. intTemp+=1;
  17. tmpNewNode.SelectSingleNode("tempcount").InnerText=intTemp.ToString();
  18. }
  19. else
  20. {
  21. //新建一个note节点来保存点击信息
  22. XmlNoderoot=document.DocumentElement;
  23. XmlElementchild=document.CreateElement("note");
  24. XmlAttributeattribute=document.CreateAttribute("newid");
  25. attribute.Value=newid.ToString();
  26. child.Attributes.Append(attribute);
  27. XmlElementchild2;
  28. child2=document.CreateElement("tempcount");
  29. child2.InnerText="1";
  30. child.AppendChild(child2);
  31. root.AppendChild(child);
  32. }
  33. document.Save(tempfile);
  34. if(flag)uptCountQueue();//如果符合条件就调用更新方法
  35. }

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

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

  1. publicstaticvoiduptCountQueue()
  2. {
  3. stringtmpFilePath=HttpContext.Current.Server.MapPath(@"~/fore/tempcount.xml");
  4. XmlReaderreader=XmlReader.Create(tmpFilePath);//xmlReader
  5. DataTableupdTable=newDataTable();
  6. DataColumncolumn=newDataColumn("info_id",typeof(int));
  7. updTable.Columns.Add(column);
  8. column=newDataColumn("info_count",typeof(Int64));
  9. updTable.Columns.Add(column);
  10. DataRowrow;
  11. while(reader.Read())
  12. {
  13. if(reader.NodeType==XmlNodeType.Element&&reader.Name=="note")
  14. {
  15. row=updTable.NewRow();
  16. XmlReaderreaderSubTree=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. if(readerSubTree.NodeType==XmlNodeType.Element&&readerSubTree.Name=="tempcount")
  25. {
  26. row[1]=reader.ReadElementContentAsInt();
  27. }
  28. }
  29. updTable.Rows.Add(row);
  30. }
  31. }
  32. reader.Close();
  33. stringfilter=null;//用来设置下面sql过滤条件
  34. foreach(DataRowrinupdTable.Rows)
  35. filter+=r[0].ToString()+",";
  36. filter=filter.Substring(0,filter.Length-1);//去处多余的“,”
  37. DataSetds=newDataSet();
  38. SqlConnectionconn=newSqlConnection(ConfigurationManager.ConnectionStrings["school_river"].ToString());
  39. SqlCommandcmd=newSqlCommand("selectinfo_id,info_countfrominfomationwhereinfo_idin("+filter+")",conn);
  40. SqlCommandcmd2=newSqlCommand("updateinfomationsetinfo_count=info_count+@info_countwhereinfo_id=@info_id",conn);
  41. SqlDataAdapteradpt=newSqlDataAdapter(cmd);
  42. adpt.UpdateCommand=cmd2;
  43. adpt.UpdateCommand.Parameter.add(NewSqlParameter("@info_count",SqlDbType.BigInt,8,"info_count"));
  44. adpt.UpdateCommand.Parameter.add(NewSqlParameter("@info_id",SqlDbType.Int,4,"info_id"));
  45. //SqlCommandBuildercmdBuilder=newSqlCommandBuilder(adpt);//SqlCommandBuilder
  46. adpt.Fill(ds);
  47. for(inti=0;i<ds.Tables[0].Rows.Count;i++)//修改获取的数据
  48. ds.Tables[0].Rows[i][1]=updTable.Rows[i][1];
  49. adpt.Update(ds.Tables[0]);//更新
  50. XmlDocumentdoc=newXmlDocument();
  51. doc.LoadXml(@"<newscount><countsum>0</countsum></newscount>");//重构tempcount.xml文件
  52. doc.Save(HttpContext.Current.Server.MapPath(@"~/fore/tempcount.xml"));
  53. }

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

<appSettings>

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

</appSettings>

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

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

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值