Memcached+net Cache

Source: Internet
Author: User
Tags connection pooling failover memcached static class

Http://www.phpfans.net/article/htmls/201008/Mjk2MTMx.html

A recent cache optimization for online projects was supposed to be written in June, but the project has been busy, giving yourself reason to be lazy

Caching considerations refer to the article more than the Earthquake Army, the article address:

Http://www.cnblogs.com/daizhj/archive/2009/11/17/1604436.html

Http://www.cnblogs.com/daizhj/archive/2009/03/23/1386652.html

I've always been on previous projects, using memcached directly or using net local caching directly, both of which are used alone

This time, the following structure was modified for the project adjustment. The main purpose is to combine the advantages of two more efficient use of caching, and secondly, to solve the Web site update code to alleviate the pressure of database query.

Let's say a few things about their pros and cons:

1.memcached has the advantages of stability, less influence by external factors and higher safety. The disadvantage is that speed is less than net cache, and the extra overhead of serialization and transmission is greater

2.net Cache, the advantage is efficient, very fast. The disadvantage is stability, poor security, which is affected by application pool recycling settings.

For the above advantages and disadvantages, decided to use the level two cache mode: client-"NET local Cache-" Memcached Cache-"Database (NET local cache defaults to 5 minutes, memcached cache time settings, generally more than 30 minutes, personally feel the end of the memcached slow

stored as a permanent cache to set, the disadvantage is the cache update strategy needs to be considered thoroughly.

In this way, when the client accesses the local cache:
There is data-> direct return
There are countless-> read downlevel cache ("memcached cache")-> have data-> returned, synchronous write-Share net cache (no data database queries, and build memcached cache)

-------------------------------------------------------------------------------------------

The above design through the actual application, the server intranet and memcached communication between the amount greatly reduced, the CPU occupies the resource aspect also relatively maintains the stable state, as before floats from 10%-30% (now is 10%-15%). The project site Solar PV is probably in 250w+

Site caching has been a preliminary completion, the above also has a great advantage is when the site restarts, especially high concurrent access, the net cache a failure, you can directly through the memcached, and will not directly cause high concurrent query database, resulting in

Database pressure is rising, if there is a SQL statement query slightly slow, it may cause the query deadlock situation appears. Ah, this is a very practical advantage. Now that the code is deployed, it is no longer a problem to update the code if the site has user access

----------------------------------------------------------------

It says so much, the introduction of the topic, C # Web site applications, the use of memcached, in the WinDOS service restart there will be a situation, the site and memcached socks connection between the failure of the site also needs to sync restart, the service can reconnect. Well, I just said it.

The high concurrent query situation causes the database to block the situation to appear again, but is the net local cache, and the memcached cache all is lost, two add up at least 3 g of data
Need to solve the above problem, restart the Web site, and restart the site is nothing but the same as the memcahced between the socks connection pool, so the key point to solve this problem is to rebuild the site and memcached between the socks connection pool. In line with this idea

, I've extended this to memcached.clientlibrary.
1.
First, the extension defines the interface ICacheManageConfig.cs

Inside defines a method that must be implemented void Memcached_restart (string logmessage); To rebuild the connection pool

2. In the call to the Memcached client component of the data layer, adding interface implementation class CacheManageConfig.cs

Code public class CacheManageConfig:Memcached.ClientLibrary.ICacheManageConfig
{
private static Memcached.ClientLibrary.SockIOPool pool = null;

<summary>
Implement interface method, memcached the realization method of client component reflection calling interface
</summary>
<param name= "Message" ></param>
public void Memcached_restart (String message)
{
_restart (message);
}

<summary>
Initializing connection pooling
</summary>
public static void Memcached_init ()
{
Memcached.ClientLibrary.MemcachedSocksMonitor.CacheManageConfigPath = "KM. Brand.DBLib.Cache.CacheManageConfig ";
The new connection pool name is used for each initialization
Memcached.ClientLibrary.MemcachedSocksMonitor.CachePoolName = "Pool" + guid.newguid (). ToString ();
String memcachedserver = system.configuration.configurationmanager.appsettings["Memcachedserver"];
string [] ServerList = {Memcachedserver};
Pool = Memcached.ClientLibrary.SockIOPool.GetInstance ( Memcached.ClientLibrary.MemcachedSocksMonitor.CachePoolName);
Pool. Setservers (serverlist);
Pool. Initconnections = 15;
Pool. Minconnections = 15;
Pool. MaxConnections = 200;
Pool. Sockettimeout = 3000;
Pool. socketconnecttimeout = 1000;
Pool. Maintenancesleep = 30;
Pool. Failover = true;
Pool. Nagle = false;
The maximum time for a single task on the socket, exceeding this time the socket will be forcibly interrupted (current task fails)
Pool. maxbusy = 1000 * 10;
Pool. Initialize ();
A connection pool that initializes a default name at the same time because the old code uses the default name of the program pool
Pool = Memcached.ClientLibrary.SockIOPool.GetInstance ();
Pool. Setservers (serverlist);
Pool. Initconnections = 10;
Pool. Minconnections = 10;
Pool. MaxConnections = 50;
Pool. Sockettimeout = 3000;
Pool. socketconnecttimeout = 1000;
Pool. Maintenancesleep = 30;
Pool. Failover = true;
Pool. Nagle = false;
The maximum time for a single task on the socket, exceeding this time the socket will be forcibly interrupted (current task fails)
Pool. maxbusy = 1000 * 10;
Pool. Initialize ();
}

#region Connection Pool Rebuild
<summary>
Connection Pool Rebuild
</summary>
<param name= "Message" ></param>
private static void _restart (String message)
{
if (pool!= null)
{//In fact, the cache management module can be integrated into this project together. Hey.
Pool. Shutdown (Memcached.ClientLibrary.MemcachedSocksMonitor.CachePoolName);
}
Re-initialize
Memcached_init ();
KM. Brand.DBLib.Cache.CacheManage.Dispose (message);
}
#endregion
}

3. Configure connection pool startup in Web application Global.asax

protected void Application_Start (object sender, EventArgs e)
{
Initializing the memcached connection pool
KM. Brand.DBLib.Cache.CacheManageConfig.Memcached_Init ();
}

4. Most critical point, modify memcached Client source code implementation, recycle connection pool

Mainly has newly established class, MemcachedSocksMonitor.cs realizes the connection pool basic operation

Code public static Class Memcachedsocksmonitor
{
private static string _cachemanageconfigpath;
private static int _socksmaxerrcount;
private static string _cachepoolname;
<summary>
Connection Pool Name
</summary>
public static string Cachepoolname
{
get {return _cachepoolname;}
set {_cachepoolname = value;}
}

<summary>
Initialization must be set
Implement the Class complete namespace path (containing the class name) for the interface Icachemanageconfig, and re-establish the method implementation name of the cache management Module Singleton instance
</summary>
public static string Cachemanageconfigpath
{
get {return _cachemanageconfigpath;}
set {_cachemanageconfigpath = value;}
}

<summary>
The conditions for the execution of the reconstruction, the number added to 20 to rebuild the connection pool, and clear zero, the cumulative number of retained 1 minutes
</summary>
public static int Socksmaxerrcount
{
get {return _socksmaxerrcount;}
set {_socksmaxerrcount = value;}
}

<summary>
Rebuilding connection Pools
</summary>
public static void Restart ()
{//Identify the need to reboot to see log error logs. Error (Getlocalizedstring ("Set IOException"), exception5);
Icachemanageconfig cachedispose = (icachemanageconfig) assembly.load (Cachemanageconfigpath).  CreateInstance ("Memcached_restart"); (icachemanageconfig) Create (disposemonthodname);
Cachedispose.memcached_restart ("The Memcached Service connection pool failed to transmit data and regenerated the connection pool.") \ r \ n cannot write data to transport connection: remote host forced to shut down an existing connection!! Number of consecutive errors in one minute "+ Socksmaxerrcount";
}

<summary>
Log the number of failed transmission errors and decide whether to rebuild the connection pool
The local cache is used here, and in general this cache does not exist, and the method rarely executes to
</summary>
public static void Sockioerrrestatr ()
{
Perform one logical process at a time
string key = "Memc

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.