About memcached applications

Source: Internet
Author: User

It is a cache and 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, specific memcache implementationCodeIsolated  If (User = Null  ) {User =Dao. getuser (userid); cacheservice. Set (  "  Userinfo:  " + Userid, user );}  Return  User ;} 

Before using memcached, You need to initialize it as follows:

 String [] Serverlist = { "  192.168.1.11: 11211  " , "  192.168.1.12: 11211  " , "  IP address 192.168.1.13: 11211  " }; //  The server can be multiple  //  Initialization pool Sockiopool = Sockiopool. getinstance (); pool. setservers (serverlist );  //  Sets the list of available cache servers in the connection pool. The server consists of IP: Port (for example, 127.0.0.1: 11211) Pool. initconnections = 3 ; //  Initial connection count Pool. minconnections = 3 ; //  Minimum connections Pool. maxconnections = 5 ; //  Max connections Pool. socketconnecttimeout = 1000 ; //  Set connection socket timeout Pool. sockettimeout = 3000 ; //  Set socket timeout read Pool. maintenancesleep =30 ; //  Set the sleep time of the maintenance thread. If it is set to 0, the maintenance thread will not start, and 30 is to wake up every 30 seconds.  //  Gets or sets the fault mark 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 it is set to false, a socket is obtained if it exists. Otherwise, null is returned if it cannot connect to the requested server. Pool. Failover = True  ; Pool. Nagle = False ; // If it is false, disableAlgorithm Pool. initialize ();

Memcachedprovider code:

Public ClassMemcachedprovider: icache {PrivateMemcachedclient MC =NewMemcachedclient ();Public VoidSet (StringKey,ObjectO) {MC. Set (Key, O );}Public ObjectGet (StringKey ){ReturnMC. Get (key );}}

 

The usage is very simple. Here are some restrictions:

    • Maximum length of each key <= 250
    • The maximum value of each value is <= 1 m (if the value exceeds 1 m, it can be resolved on the C # end by itself)
    • There is no redundancy mechanism. When a memcached server is down, data is lost and data is not automatically balanced.

UML diagram of the example:

The cached data is subsequently distributed to each memcache server through hash. Therefore, if each memcached server is allocated to 4 GB memory separately, then our C #ProgramYou can use 12 GB of memory. This statement is not accurate. Whether the 12 GB memory can be used is sufficient depends on the Distributed Key Algorithm of memcacheclient (the left-side interface in the middle part), and the algorithm will be further studied later.

The following describes the solution after a memcached server is down (three solutions in total ):
    1. Hot Standby Node
    2. Remove the invalid memcached Server IP address from the configuration file.
    3. Add a backup memcached Server

 

I. Use hot standby nodes

The program does not change anything.

When a memcached server is found to be down, start a new memcached server and change the IP address of the new memcached server to the IP address of the down server (do not fix the IP address of the down server separately)

The disadvantage of this method is that the cache does not exist during the down and going online, and throw exception occurs. It needs to be captured by the program and processed as a null value, which increases the pressure on the database.

 

2. Remove the invalid memcached Server IP address from the configuration file.

That is to modify the configuration file, and then slowly fix the down server. After the repair, modify the configuration file...

Very bad. Modifying the configuration file will cause application reset. However, this is the only option when resources are limited.

 

3. Add a backup memcached Server

The physical node diagram is as follows:

C # client: changed from an original memcachedclient client to "memcachedclient" and "backupmemcachedclient ".

Memcached server: adds a backup memcached server. The performance and memory of this server are higher than the configurations of the other three memcached servers. This server will save all the caches, so it has high performance requirements, especially network and memory.

Initialize the two pools first:

 String [] Serverlist = { "  192.168.1.11: 11211 " , "  192.168.1.12: 11211  " , "  IP address 192.168.1.13: 11211  " }; // General memcached Server  //  Initialization pool Sockiopool pool = sockiopool. getinstance ( "  Normalservers  " );// Set poolnamePool. setservers (serverlist );  //  Sets the list of available cache servers in the connection pool. The server consists of IP: Port (for example, 127.0.0.1: 11211) Pool. initconnections = 3 ; //  Initial connection count Pool. minconnections = 3 ; //  Minimum connections Pool. maxconnections = 5 ; // Max connections Pool. socketconnecttimeout = 1000 ; //  Set connection socket timeout Pool. sockettimeout = 3000 ; //  Set socket timeout read Pool. maintenancesleep = 30 ; //  Set the sleep time of the maintenance thread. If it is set to 0, the maintenance thread will not start, and 30 is to wake up every 30 seconds.  //  Gets or sets the fault mark 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 it is set to false, a socket is obtained if it exists. Otherwise, null is returned if it cannot connect to the requested server. Pool. Failover = True  ; Pool. Nagle = False ; //  If false, disable the Nagle Algorithm for all created sockets.  Pool. initialize ();  String [] Backupserverlist = { "  IP address 192.168.1.14: 11211 "  };// Backup Server  //  Initialization pool Sockiopool backuppool = sockiopool. getinstance ( "  Backupservers  "  );// Set poolnameBackuppool. setservers (backupserverlist );  //  Sets the list of available cache servers in the connection pool. The server consists of IP: Port (for example, 127.0.0.1: 11211) Backuppool. initconnections = 3 ;//  Initial connection count Backuppool. minconnections = 3 ; //  Minimum connections Backuppool. maxconnections = 5 ; //  Max connections Backuppool. socketconnecttimeout = 1000 ; //  Set connection socket timeout Backuppool. sockettimeout = 3000 ;//  Set socket timeout read Backuppool. maintenancesleep = 30 ; //  Set the sleep time of the maintenance thread. If it is set to 0, the maintenance thread will not start, and 30 is to wake up every 30 seconds.  //  Gets or sets the fault mark 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 it is set to false, a socket is obtained if it exists. Otherwise, null is returned if it cannot connect to the requested server. Backuppool. Failover = True ; Backuppool. Nagle = False ; //  If false, disable the Nagle Algorithm for all created sockets. Backuppool. initialize ();

 

Change memcachedprovider:

 Public   Class  Memcachedprovider: icache {  Private   Static  Memcachedclient getnormalserverclient () {memcachedclient MC = New Memcachedclient (); Mc. poolname = "  Normalservers  "  ;  Return  MC ;}  Private   Static  Memcachedclient getbackupserverclient () {memcachedclient MC = New  Memcachedclient (); Mc. poolname = " Backupservers  "  ;  Return  MC ;}  Public   Void Set ( String Key, Object  O ){  Try  {Getnormalserverclient (). Set (Key, O); getbackupserverclient (). Set (Key, O );// This is changed to asynchronous execution.} Catch  {  //  Y or log  }}  Public   Object Get ( String  Key ){  Object O = Null  ;  Try  {O =Getnormalserverclient (). Get (key );}  Catch  {  //  Y or log  }  If (O! = Null  )  Return  O;  Try  {O =Getbackupserverclient (). Get (key );}  Catch  {  //  Y or log  }  Return  O ;}} 

That's all.

 

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.