About the. net implementation of the Memcache mutex Design Mode

Source: Internet
Author: User
I have previously read the scenario analysis and implementation code of memcache-mutex on the Internet. Here I will implement the. net method. Of course, this is mainly based on the original pseudocode as a summary and record. If you are interested in the implementation, try to use the Code provided in this article for testing. If you have any questions, please contact me in time. Link: http://timyang.net/programming/memcache-mutex/
Local Link: http://www.cnblogs.com/daizhj/articles/1959704.html

 

To implement the object expiration time attribute in the original text, a base class is defined. Its information is as follows:

 

[Serializable]
Public class CacheObj
{
/// <Summary>
/// Absolute data expiration time. The default value is three minutes after the current time.
/// </Summary>
Public DateTime ExpireTime = DateTime. Now. AddMinutes (3 );

/// <Summary>
/// Relatively valid data time, in seconds. The default value is 30 seconds.
/// </Summary>
Public int TimeOut = 30;
}

In this way, all objects to be put into memcached can be inherited as long as they are inherited. For example, the following user information class:

/// <Summary>
/// User information
/// </Summary>
[Serializable]
Public class UserInfo: CacheObj
{
Public string UserName;
Public int Age;
Public string Email;

Public override string ToString ()
{
Return "UserName:" + UserName + "Age:" + Age + "Email:" + Email;
}
}

The following is the implementation code of method 1 in the original article:

 

MemcachedClient mc = MemCachedManager. CacheClient;
// Fang Yi
Public UserInfo GetCacheData1 (string key)
{
UserInfo value = mc. Get (key) as UserInfo;
If (value = null)
{
// Expires in 3 minutes. The current key_mutex add can only be added once before the delete operation is executed and true is returned.
If (mc. Add (key + "_ mutex", key + "_ mutex", DateTime. Now. AddMinutes (3) = true)
{
Value = new UserInfo () {UserName = "daizhj", Email = "daizhj617595@126.com"}; // db. get (key); // load data from
Mc. Set (key, value );
Mc. Delete (key + "_ mutex ");
}
Else
{
System. Threading. Thread. Sleep (500); // if the setting is too short, the above set syntax may not take effect.
Value = mc. Get (key) as UserInfo; // read the cache data again after sleep
}
}
Return value;
}

The code for method 2 is as follows:

 

// Method 2
Public UserInfo GetCacheData2 (string key)
{
UserInfo value = mc. Get (key) as UserInfo;
If (value = null)
{
// Expires in 3 minutes. The current key_mutex add can only be added once before the delete operation and returns true
If (mc. Add (key + "_ mutex", "add_mutex", DateTime. Now. AddMinutes (3) = true)
{
Value = new UserInfo () {UserName = "daizhj", Email = "daizhj617595@126.com"}; // db. get (key); // load data from
Mc. Set (key, value );
Mc. Delete (key + "_ mutex ");
}
Else
{
System. Threading. Thread. Sleep (500); // if the setting is too short, the above set syntax may not take effect.
Value = mc. Get (key) as UserInfo; // read the cache data again after sleep
}
}
Else
{
If (value. ExpireTime <= DateTime. Now)
{
// It has a value but has expired
If (mc. Add (key + "_ mutex", "add_mutex", DateTime. Now. AddMinutes (3) = true)
{
Value. ExpireTime = DateTime. Now. AddSeconds (value. TimeOut );
// This is only to make it valid for the time being (the expired data will be updated later). This mainly prevents a large number of requests from obtaining mutex and performing sleep when the cache becomes invalid, note that setting this parameter to valid causes other threads to temporarily read dirty data.
Mc. set (key, value, DateTime. now. addSeconds (value. timeOut * 2); // here * 2 is used to make memcached cache data longer, because ExpireTime is used to determine the actual verification expiration time.

// Load the latest data from the data source
Value = new UserInfo () {UserName = "daizhenjun", Email = "617595@163.com"}; // db. get (key );
Value. ExpireTime = DateTime. Now. AddSeconds (value. TimeOut );
Mc. Set (key, value, DateTime. Now. AddSeconds (value. TimeOut * 2 ));
Mc. Delete (key + "_ mutex ");
}
Else
{
System. Threading. Thread. Sleep (500); // if the setting is too short, the above set syntax may not take effect.
Value = mc. Get (key) as UserInfo; // read the cache data again after sleep
}
}
}
Return value;
}

No matter which method is used, it will increase the complexity of the Code (especially the second one). In addition, there is an additional connection with memcached and storage overhead (the key_mutex storage also consumes resources ). Unless memcached is updated at the same time in a high-concurrency scenario, the two methods must be considered.

 

Source code:/Files/daizhj/MemcachedApp.rar

[Example is in MemcachedApp \ sample \ MutexSample. aspx]

 

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.