Before on-line saw Memcache-mutex's scene analysis and implementation code, here the. NET way to implement, of course, here is mainly based on the original pseudo-code tiger, as a summary and record. If you are interested in the implementation you can try using the code provided in this article to test, if there is a problem, please contact me in time. Original link: http://timyang.net/programming/memcache-mutex/Local link: http://www.cnblogs.com/daizhj/articles/1959704.html
In order to implement the object expiry time attribute in the source text, a base class is defined with the following information:
[Serializable]
public class Cacheobj
{
<summary>
Data absolute expiry time, defaults to three minutes after the current time starts to expire
</summary>
Public DateTime expiretime = DateTime.Now.AddMinutes (3);
<summary>
Data relative effective time, in seconds. Default is 30 second validity
</summary>
public int TimeOut = 30;
}
So all objects to be placed in the memcached are OK, as long as they inherit the object, such as 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 mode one in the original text:
memcachedclient mc = memcachedmanager.cacheclient;
Fang Yi
Public UserInfo GetCacheData1 (string key)
{
UserInfo value = mc. Get (key) as UserInfo;
if (value = = null)
{
3 minutes expires. The current Key_mutex add can only be added once and return true before the delete operation executes
if (MC. ADD (key + "_mutex", key + "_mutex", DateTime.Now.AddMinutes (3)) = = True)
{
Value = new UserInfo () {UserName = "daizhj", email = "[email protected]"};//db.get (key);//Load data from
Mc. Set (key, value);
Mc. Delete (key + "_mutex");
}
Else
{
System.Threading.Thread.Sleep (500);//If Set too short, the above set syntax may not be valid
Value = MC. Get (key) as Userinfo;//sleep retry reading cache data
}
}
return value;
}
Here's the code for Mode 2:
Method Two
Public UserInfo GetCacheData2 (string key)
{
UserInfo value = mc. Get (key) as UserInfo;
if (value = = null)
{
3 minutes expires, before delete, the current Key_mutex add can only be added once and return True
if (MC. ADD (key + "_mutex", "Add_mutex", DateTime.Now.AddMinutes (3)) = = True)
{
Value = new UserInfo () {UserName = "daizhj", email = "[email protected]"};//db.get (key);//Load data from
Mc. Set (key, value);
Mc. Delete (key + "_mutex");
}
Else
{
System.Threading.Thread.Sleep (500);//If Set too short, the above set syntax may not be valid
Value = MC. Get (key) as Userinfo;//sleep retry reading cache data
}
}
Else
{
if (value. Expiretime <= DateTime.Now)
{
has 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 just to make it temporarily valid (the outdated data will be updated later), which is primarily to prevent the cache from failing at a time when a large number of requests to obtain a mutex and sleep, note that this is set to a valid cause other threads will temporarily read dirty data
Mc. Set (key, Value, DateTime.Now.AddSeconds (value). TimeOut * 2);//This is to let memcached cache the data for a longer period of time, because the true check expiry time with Expiretime to judge
Load up-to-date data from a data source
Value = new UserInfo () {UserName = "Daizhenjun", email = "[email protected]"};//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 Set too short, the above set syntax may not be valid
Value = MC. Get (key) as Userinfo;//sleep retry reading cache data
}
}
}
return value;
}
Either way, the complexity of the code is increased (especially the second), and there is additional connection and storage overhead associated with memcached (Key_mutex itself consumes resources as well). These two methods need to be used as well unless the memcached is updated simultaneously under high concurrency scenarios.
Source:/files/daizhj/memcachedapp.rar
"Example in Memcachedapp\sample\mutexsample.aspx"
About the. NET implementation of the Memcache mutex design pattern