After System. Web. Caching. Cache deletes a key, it wants to trigger other actions at the same time (the critical moment is the event's hold)

Source: Internet
Author: User

System. web. caching. cache is. net provides us with a lightweight Cache component that provides the most basic addition, expiration, deletion, and retrieval operations. The following is an encapsulation of the Cache, this facilitates future Unified calling and expansion.
/// <Summary>
/// Slow o storage? Xiangà Guan? ? Operate as an operator.
/// What is the percentage? ?
/// </Summary>
Public class DataCache
{
/// <Summary>
/// Get? ? When ± anterior ° | use? Which of the following statements about the progress sequence? Are you sure you want to know about CacheKey? Cache value μ
/// </Summary>
/// <Param name = "CacheKey"> </param>
/// <Returns> </returns>
Public static object GetCache (string CacheKey)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
Return objCache [CacheKey];

}
/// <Summary>
/// Set? When ± anterior ° | use? Which of the following statements about the progress sequence? Are you sure you want to know about CacheKey? Cache value μ
/// Used for commissioning? : ODataCache. SetCache ("name", "zzl ");
/// </Summary>
/// <Param name = "CacheKey"> </param>
/// <Param name = "objObject"> </param>
Public static void SetCache (string CacheKey, object objObject)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
ObjCache. Insert (CacheKey, objObject );
}

/// <Summary>
/// Set? When ± anterior ° | use? Which of the following statements about the progress sequence? Are you sure you want to know about CacheKey? Cache value μ
/// Used for commissioning? : ODataCache. SetCache ("name", "zzl", DateTime. Now. AddMinutes (1), TimeSpan. Zero );
/// </Summary>
/// <Param name = "CacheKey"> </param>
/// <Param name = "objObject"> </param>
Public static void SetCache (string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
ObjCache. Insert (CacheKey, objObject, null, absoluteExpiration, slidingExpiration );
}

/// <Summary>
/// Shift? Except y? Dingju Cache
/// </Summary>
/// <Param name = "CacheKey"> </param>
Public static void RemoveCache (string CacheKey)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
ObjCache. Remove (CacheKey );

}

# Region Events
/// <Summary>
/// Slow o storage? Delete? Except y? Parts t
/// </Summary>
Public static event CacheEventHandler CacheDeleted;
# Endregion

# Region Methods
/// <Summary>
/// Trigger $ initiate slow o storage? Delete? Except y? Parts t
/// </Summary>
Public static void OnCacheDeleted (string key)
{
If (CacheDeleted! = Null ))
{
CacheDeleted (null, new CacheEventArgs (key ));
}

Remove (key );

}
# Endregion
}

# Region Delegates
/// <Summary>
/// Slow o storage? Commissioned by D
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Public delegate void CacheEventHandler (object sender, CacheEventArgs e );
# Endregion

/// <Summary>
/// Slow o storage? Things? Device t source ′
/// </Summary>
Public class CacheEventArgs
{
Public CacheEventArgs ()
{

}
Public CacheEventArgs (string cacheKey)
{
This. CacheKey = cacheKey;
}
Public string CacheKey {get; set ;}
}

In this class, there is an event about deleting a key. Its function is to perform other operations associated with it when deleting a key. For example, when a user logs out, we want to clear its cache records and delete some record operations, but the record operations are not fixed. To destroy the program integrity, we can log out, subscribe to the required operations first. If there are other associated operations required to delete the cache key in the future, you only need to subscribe to them.
The Code is as follows:
Static CommonMethod ()
{
IUserRepository = new UserRepository ();
// Order? Reading? ? CacheDeleted? Things? T ,? When ± U is? When you trigger ¥, + ,? Yes? ? When the operator executes 'Row D is? Bound? Fang? Famo
DataCache. CacheDeleted + = new CacheEventHandler (ClearUserRecord );
DataCache. CacheDeleted + = new CacheEventHandler (ClearProductRecord );
}
# Region deletion? After yCache ,? Do you want a to do it? ? Which? U thing? Iné
Static void ClearUserRecord (object sender, CacheEventArgs e)
{
DataCache. SetCache ("msg1", e. CacheKey + "? Delete? Except y? ");
}


Static void ClearProductRecord (object sender, CacheEventArgs e)
{
DataCache. SetCache ("msg2", e. CacheKey + "true? ? By? Delete? Except y? ");
}
# Endregion


After logging out, you can trigger the operation to delete the cache key:
 
Public static ActionResult UserLogOutModule (Controller controller)
{
String returnUrl = controller. Request. QueryString ["returnUrl"];
Standard. ClientHelper. UserLogout ();
DataCache. OnCacheDeleted ("name ");


From Lose. zhang

Related Article

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.