使用MongoDB存储访问者信息
2010-07-10 17:36
453 查看
网站的访问者信息的存储一般都是海量的,通常使用关系数据库,现在NoSQL运动火热,满足这样的需求使用NoSQL数据库会更好,网站访问者信息主要是两个功能:
1、记录下网站的访问者信息
2、查询访问者信息和做相关的数据分析
本文采用MongoDB来记录访问者的信息的示例:
在asp.net中记录访问者信息的方法可以通过一个HttpHandler,在页面上放一个1像素的图片来请求这个HttpHandler,把他放到MasterPage页面就可以了。
下面给出ashx的代码
[/code]
上面代码使用到了HttpBrowserCapabilities,这里可以得到客户端的浏览器信息。还有客户端ip的来源使用到了ipinfodb.com这个服务,IPinfoDB网站非常的慷慨,慷慨到让人惊讶的程度,除了提供给你XML API和JSON API调用外,还提供了实现这些API的source code和所有的IP数据库,也就是说你只要下载这份code和database你也可以架设一个和IPinfoDB一样的网站,一样能够提供API服务,不过国外做的IP数据库对国内来说肯定不是很全很准,不过先将就着用吧。
访问访问者信息的MongoDB的信息记录:
[/code]
然后利用MongoDB的C# NORm驱动记录到MongoDB。
相关文章:
/article/4588113.html
http://www.eggheadcafe.com/tutorials/aspnet/63de8012-127a-4478-8725-3e1c27969596/nosql-mongodb-install-lotus-notes-and-couchdb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/51d3ae19-d6f9-4807-ac0a-0baab2964b03/mongodb-install-as-service-and-use-net-drivers.aspx
http://www.eggheadcafe.com/tutorials/aspnet/93206c89-09c9-40fc-9296-7d74bb7996ad/a-mongodb-cache-utility.aspx
http://www.eggheadcafe.com/tutorials/aspnet/3a73c6de-82a1-4690-a7aa-d0eda58203f7/store-aspnet-site-visitor-stats-in-mongodb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/27f836b7-2c9e-4942-9712-1c7b901cadcc/aspnet-providerless-custom-forms-authentication-roles-and-profile-with-mongodb.aspx
1、记录下网站的访问者信息
2、查询访问者信息和做相关的数据分析
本文采用MongoDB来记录访问者的信息的示例:
在asp.net中记录访问者信息的方法可以通过一个HttpHandler,在页面上放一个1像素的图片来请求这个HttpHandler,把他放到MasterPage页面就可以了。
下面给出ashx的代码
[code] public class a : IHttpHandler
{
public void ProcessRequest(HttpContext ctx)
{
HttpBrowserCapabilities bc = ctx.Request.Browser;
Stat stat = new Stat();
stat._id = Guid.NewGuid();
stat.Browser = bc.Browser;
stat.Type = bc.Type;
stat.Version = bc.Version;
stat.Platform = bc.Platform;
stat.UrlReferrer = ctx.Request.UrlReferrer.ToString();
stat.UserHostAddress = ctx.Request.UserHostAddress;
stat.HttpMethod = ctx.Request.HttpMethod;
stat.IsAuthenticated = ctx.Request.IsAuthenticated;
stat.LogDateTime = DateTime.Now.ToLocalTime();
WebClient wc=new WebClient();
try
{
string s =
wc.DownloadString("http://ipinfodb.com/ip_query.php?ip=" + stat.UserHostAddress + "&output=xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
stat.Country = doc.DocumentElement.SelectNodes("CountryCode")[0].InnerText;
stat.State = doc.DocumentElement.SelectNodes("RegionName")[0].InnerText;
stat.City = doc.DocumentElement.SelectNodes("City")[0].InnerText;
stat.Latitude = doc.DocumentElement.SelectNodes("Latitude")[0].InnerText;
stat.Longitude = doc.DocumentElement.SelectNodes("Longitude")[0].InnerText;
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message+ ex.StackTrace );
}
finally
{
wc.Dispose();
}
using (Mongo mongo = Mongo.Create(Helper.ConnectionString() ))
{
MongoCollection<Stat> coll = (MongoCollection<Stat>)mongo.GetCollection<Stat>();
coll.Save(stat);
}
string sFileName = String.Empty;
string sPath = ctx.Server.MapPath(".");
try
{
sFileName = ctx.Request["name"].ToString().Trim();
if (sFileName.Length < 5){ return;} // must be at least "1.gif" (5 chars)
// serve the image that was requested:
ctx.Response.WriteFile(sPath + @"\" + sFileName);
}
catch (Exception e)
{
ctx.Response.Write(e.Message);
}
}
public bool IsReusable{ get{ return true;}}
}
[/code]
上面代码使用到了HttpBrowserCapabilities,这里可以得到客户端的浏览器信息。还有客户端ip的来源使用到了ipinfodb.com这个服务,IPinfoDB网站非常的慷慨,慷慨到让人惊讶的程度,除了提供给你XML API和JSON API调用外,还提供了实现这些API的source code和所有的IP数据库,也就是说你只要下载这份code和database你也可以架设一个和IPinfoDB一样的网站,一样能够提供API服务,不过国外做的IP数据库对国内来说肯定不是很全很准,不过先将就着用吧。
访问访问者信息的MongoDB的信息记录:
[code] [Serializable]
public class Stat
{
[MongoIdentifier]
public Guid _id{get;set;}
public string Type{get;set;}
public string Browser{get;set;}
public string Version{get;set;}
public string Platform{get;set;}
public string UrlReferrer{get;set;}
public string UserHostAddress{get;set;}
public bool IsAuthenticated{get;set;}
public string HttpMethod{get;set;}
public DateTime LogDateTime{ get; set;}
public string City{ get; set;}
public string State{ get; set;}
public string Country{ get; set;}
public string Latitude{ get; set;}
public string Longitude{ get; set;}
}
[/code]
然后利用MongoDB的C# NORm驱动记录到MongoDB。
相关文章:
/article/4588113.html
http://www.eggheadcafe.com/tutorials/aspnet/63de8012-127a-4478-8725-3e1c27969596/nosql-mongodb-install-lotus-notes-and-couchdb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/51d3ae19-d6f9-4807-ac0a-0baab2964b03/mongodb-install-as-service-and-use-net-drivers.aspx
http://www.eggheadcafe.com/tutorials/aspnet/93206c89-09c9-40fc-9296-7d74bb7996ad/a-mongodb-cache-utility.aspx
http://www.eggheadcafe.com/tutorials/aspnet/3a73c6de-82a1-4690-a7aa-d0eda58203f7/store-aspnet-site-visitor-stats-in-mongodb.aspx
http://www.eggheadcafe.com/tutorials/aspnet/27f836b7-2c9e-4942-9712-1c7b901cadcc/aspnet-providerless-custom-forms-authentication-roles-and-profile-with-mongodb.aspx
相关文章推荐
- 使用MongoDB存储访问者信息
- 使用 MongoDB 存储商品分类信息
- MongoDB应用案例:使用 MongoDB 存储商品分类信息
- 使用Keychain存储用户敏感信息
- Yii中使用MySQL存储Session信息
- 使用配置文件来存储数据库链接信息时---工具类的编写
- 使用MongoDB的MapReduce进行区域地震信息统计实验
- 使用变量存储以下MP3信息,并打印输出
- 使用HtmlAgilityPack爬取网站信息并存储到mysql
- 使用Mongodb存储上传物理文件并进行SQUID加速(基于aspx页面)
- 使用数组来模拟邻接表存储图信息
- 在J2ME中使用记录存储系统(RMS)存储信息
- 商城项目-使用json模板实现带分类的产品规格信息存储及展示
- MongoDB在java中的操作-添,删,查 基本数据 文件存储 可使用
- 使用Forms验证存储用户自定义信息
- J2ME中使用记录存储系统(RMS)存储信息
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 在J2ME中使用记录存储系统(RMS)存储信息
- 使用MongoDB存储Docker日志
- mongodb底层存储和索引原理——本质是文档数据库,无表设计,同时wiredTiger存储引擎支持文档级别的锁,MMAPv1引擎基于mmap,二级索引(二级是文档的存储位置信息『文件id + 文件内offset 』)