using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Caching;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace TGBUS.Count
{
public class TGBUS_Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int _pid = 0;
try
{
if (context.Request.QueryString["pid"] != null && context.Request.QueryString["pid"].ToString() != "")
{
_pid = Convert.ToInt32(context.Request.QueryString["pid"].ToString());
}
CounterHelper objCounterHelper = new CounterHelper(_pid.ToString());
objCounterHelper.AddHits();
//context.Response.Write(string.Format("document.write('nums:{0} id:{1}')", objCounterHelper.AllHits,context.Request.Url.AbsoluteUri));
}
catch(Exception ex)
{ }
}
public bool IsReusable
{
get
{
return true;
}
}
}
#region 统计类
public class CounterHelper
{
public int _Hits = 0; //累计的点击数
private static int _HitsAll = 0;
private int ProductId = 0;
private string CacheName = "TGBUS_Hits";//缓存名称
private object LockForAddHits = new object();//LockForAddHits锁
private object LockForItemRemovedFromCacheHits = new object();//ItemRemovedFromCacheHits锁
CacheItemRemovedCallback onRemove = null;//CacheItemRemovedCallback对象
/**/
/// <summary>
/// 构造函数
/// </summary>
public CounterHelper(string _ProductId)
{
CacheName = CacheName + _ProductId;
ProductId = Convert.ToInt32(_ProductId);
HttpContext ctx = HttpContext.Current;
LoadHits();
}
/// <summary>
/// 加载点击数
/// </summary>
private void LoadHits()
{
if (HttpRuntime.Cache[CacheName] == null)
{
_Hits = 0;
}
else
{
_Hits = Convert.ToInt32(HttpRuntime.Cache["_temp" + CacheName].ToString());
}
}
/// <summary>
/// 将累计点击数保存到全局变量,当它达到一定量时保存到文本文件,并清空
/// </summary>
public void AddHits()
{
lock (LockForAddHits)
{
if (HttpRuntime.Cache[CacheName] != null)
{
Add();
_HitsAll = Hits;
if (Hits > 500)
{
//--移除
HttpRuntime.Cache.Remove(CacheName);
}
}
else
{
onRemove = new CacheItemRemovedCallback(ItemRemovedFromCache);
HttpRuntime.Cache.Insert(
CacheName,
"This Object For Expired",
null,
DateTime.Now.AddSeconds(20),
TimeSpan.Zero,
System.Web.Caching.CacheItemPriority.Normal,
onRemove
);
Add();
_HitsAll = Hits;
}
}
}
/**/
/// <summary>
/// 当缓存被移除或过期是触发的回调事件
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="reason"></param>
private void ItemRemovedFromCache(string key, object value, CacheItemRemovedReason reason)
{
try
{
#region 清空并写到数据库
int hits = 0;
lock (LockForItemRemovedFromCacheHits)
{
hits = _HitsAll;
}
if (hits == 0)
{
return;
}
else
{
SaveHits(hits);
}
#endregion
}
catch (Exception ex)
{
}
}
/// <summary>
/// 保存点击数
/// </summary>
/// <param name="hits"></param>
private void SaveHits(int hits)
{
string _Sql = "Update dbo].[Product] set [ViewsCount] = [ViewsCount]+" + hits + " where [ProductId]='" + ProductId + "'";
TGBUS.DBUtility.SqlHelper.ExecuteNonQuery(TGBUS.DBUtility.SqlHelper.connectionString, CommandType.Text, _Sql);
}
/// <summary>
/// 累加点击数
/// </summary>
private void Add()
{
_Hits = _Hits + 1;
HttpRuntime.Cache["_temp" + CacheName] = _Hits;
}
//// <summary>
/// 获取所有的点击数
/// </summary>
public int AllHits
{
get
{
return Hits;
}
}
/**/
/// <summary>
/// 获取累计的点击数
/// </summary>
private int Hits
{
get { return _Hits; }
set { _Hits = value; }
}
}
#endregion
}