您的位置:首页 > 编程语言 > ASP

利用 ASP.NET 的Timer 来实现的访问统计,感觉比较适合高访问量的网站

2007-04-21 01:49 666 查看
首先是因为晚上我要把我的博客的访问统计加上去的.起初的想法很简单 随便在 Session_Start 里面,自动往数据表里加 1

但考虑到如果访问人数很高的话是不是每次都很频烦的访问(还好我的访问量并不高

).

后来想到在Application_Start的时候存数据到 缓存里,Application_End 存进数据表不就得了,.貌似这个想法不错.但有一个问题,就是不可预测的因素,什么服务突然怎么怎么样啦.所以现在我的做法是,把数据 保存在 缓存里 然后利用timer间隔一定时间往数据表里写.

static Timer timer=new Timer(100 * 60*30);

void Application_Start(object sender, EventArgs e)
{
/---省略---/

string dtc = "1";
if (dr.Read())
{
dtc = dr["vr_count"].ToString();
}
dr.Close();

Application["total"] = dtc;

timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 100 * 60 * 30;
timer.Enabled = true;
}

void timer_Elapsed(object sender, ElapsedEventArgs e)
{

int total = Application["total"] != null ? Convert.ToInt32(Application["total"].ToString()) : 0;
if (total != 0)
{
/--数据更新--/
}
}

void Session_Start(object sender, EventArgs e)
{

if (Application["total"] == null)
{
int total=1;
Application.Lock();
Application["total"] = total;
Application.UnLock();
}
else
{
int total = Application["total"] != null ? Convert.ToInt32(Application["total"].ToString()) : 0;
Application.Lock();
total++;
Application["total"] = total;
Application.UnLock();
}

}

很简单,,但效果已经达到了,,不知道这样做有没有什么不足,,望指教...
http://dubox.cn/content-44.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: