Talking about the application of memcached

Source: Internet
Author: User

is a cache that can set, get value, haha, common usage is as follows:

Public User getuser (int userId)
{
            UserInfo user = Cacheservice.get ("UserInfo:" + userId) as userinfo;// Cacheservice is a custom interface, and the specific Memcache implementation code is quarantined with the
            if (user = null)
            {
                user = Dao. GetUser (userId);
                Cacheservice.set ("UserInfo:" + userId, user);
            }
            return user;
}

Before you actually use memcached, you need to initialize it, as follows:

String[] ServerList = {"192.168.1.11:11211", "192.168.1.12:11211", "192.168.1.13:11211"};//server can be multiple  
    
            //initialization pools  
            Sockiopool pool = sockiopool.getinstance ();
            Pool. Setservers (serverlist)//sets the list of cache servers available for connection pooling, and the server is composed of ip:port (such as: 127.0.0.1:11211)  
            pool. Initconnections = 3;//Initial connection number  
            pool. minconnections = 3;//Minimum connection number  
            pool. MaxConnections = 5;//Maximum number of connections  
            pool. Socketconnecttimeout = 1000;//Sets the socket timeout pool for the connection  
            . Sockettimeout = 3000;//Sets the socket timeout read  
            pool. Maintenancesleep = 30;//Sets the sleep time that the maintenance thread is running. If set to 0, the maintenance thread will not start, 30 is to wake up every 30 seconds  
    
            //Get or set the fault flag of the pool.  
            //If this flag is set to true the socket connection fails and an attempt is made to return a socket from another server if it exists.  
            //If set to False, get a socket if it exists. Otherwise null is returned if it cannot connect to the requested server.  
            pool. Failover = true;
    
            Pool. Nagle = false;//If False, closes the Nagle algorithm pool for all created sockets  
            . Initialize ();

Memcachedprovider Code:

public class Memcachedprovider:icache
    {
        private memcachedclient mc = new Memcachedclient ();
        public void Set (string key, Object o)
        {
            mc. Set (key, O);
        }
    
        public object get (string key)
        {return
            MC. Get (key);
        }
    

The usage is simple, and here are some of its limitations:

Maximum <=250 length per key

Maximum <=1m per value (if more than 1M, then decompose itself in C #)

No redundancy mechanism, both: When a memcached server down, the data is lost, does not automatically balance the data

UML Diagram of Sample:

The cached data is then distributed to each of the Memcache servers through the hash, so that our C # program can use 12G memory, assuming each memcached server is allocated to 4G of memory respectively. This statement is not accurate, can use the 12G of memory, fully see the memcacheclient end of the distribution key algorithm (the left side of the image in the interface section), and then further study the algorithm.

Here's how to fix a memcached server down (a total of 3 solutions):

Using hot standby node

Remove the IP from the failed memcached server from the configuration file

Add a backup Memcached Server

One, with hot standby node

The program end doesn't change anything.

When a memcached server down machine is found, a new memcached server is started and the IP of the new memcached server is changed to the IP of Down machine (the down one is not to be repaired separately)

The disadvantage of this approach is: in down and on the line, the cache is not, will throw exception, need program capture, and as a null value to handle, will increase the pressure of the database

Two, from the configuration file to remove the invalid memcached server IP

That is to modify the configuration file, and then put down the server slowly repaired, and so on repair and then modify the configuration file ...

Very bad, light modification configuration file will cause application reset. But when resources are limited, this is the only way.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

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.