Database update when the specified number of times or cache expires

Source: Internet
Author: User

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 statistics
Public class CounterHelper
{
Public int _ Hits = 0; // The total number of clicks
Private static int _ HitsAll = 0;
Private int ProductId = 0;
Private string CacheName = "TGBUS_Hits"; // cache name
Private object LockForAddHits = new object (); // LockForAddHits lock
Private object LockForItemRemovedFromCacheHits = new object (); // ItemRemovedFromCacheHits lock
CacheItemRemovedCallback onRemove = null; // CacheItemRemovedCallback object

/**/
/// <Summary>
/// Constructor
/// </Summary>
Public CounterHelper (string _ ProductId)
{
CacheName = CacheName + _ ProductId;
ProductId = Convert. ToInt32 (_ ProductId );
HttpContext ctx = HttpContext. Current;
LoadHits ();
}
/// <Summary>
/// Load the number of clicks
/// </Summary>
Private void LoadHits ()
{
If (HttpRuntime. Cache [CacheName] = null)
{
_ Hits = 0;
}
Else
{
_ Hits = Convert. ToInt32 (HttpRuntime. Cache ["_ temp" + CacheName]. ToString ());
}
}
/// <Summary>
/// Save the cumulative number of clicks to the global variable. When it reaches a certain number, save it to the text file and clear it.
/// </Summary>
Public void AddHits ()
{
Lock (LockForAddHits)
{
If (HttpRuntime. Cache [CacheName]! = Null)
{
Add ();
_ HitsAll = Hits;
If (Hits> 500)
{
// -- Remove

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>
/// The callback event triggered when the cache is removed or expired
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
/// <Param name = "reason"> </param>
Private void ItemRemovedFromCache (string key, object value, CacheItemRemovedReason reason)
{
Try
{
# Region clear and write to database
Int hits = 0;
Lock (LockForItemRemovedFromCacheHits)
{
Hits = _ HitsAll;
}
If (hits = 0)
{
Return;
}
Else
{
SaveHits (hits );
}
# Endregion
}
Catch (Exception ex)
{

}
}

/// <Summary>
/// Save the number of clicks
/// </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>
/// Accumulate clicks
/// </Summary>
Private void Add ()
{
_ Hits = _ Hits + 1;
HttpRuntime. Cache ["_ temp" + CacheName] = _ Hits;
}
//// <Summary>
/// Obtain all clicks
/// </Summary>
Public int AllHits
{
Get
{
Return Hits;
}
}

/**/
/// <Summary>
/// Obtain the total number of clicks
/// </Summary>
Private int Hits
{
Get {return _ Hits ;}
Set {_ Hits = value ;}
}
}
# Endregion
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.