Use of memcached

Source: Internet
Author: User
Tags failover memcached

A brief introduction to Memcache
High-performance distributed memory object cache system, read and write speed is very fast, to solve the pain of Io read and write slow, it is mainly considered performance, no disaster tolerance function, when the cache server hangs, the data will be lost all. The value of the key in Memcache is 255 characters, it will partition the memory that can be used, then block each area, each block has 1 m, it is best not to store more than 1M, when a cached data is stored in a block, the next data will be stored in a new block, So there is a waste of some resources
In Memcache, there is no cache dependency and time detection, but lazy deletion, when querying the data, to see if the data is out of date, if it expires, the data is deleted, the data is deleted just delete the data is not deleted in memory fast, so as to avoid the generation of memory fragmentation. When the memory is exhausted, the new data is stored in, first check whether there is an idle memory block, but there is no time to check if there is a data bulge if it expires, then the data is deleted, and then stored in new data, if there is no expired data, will be used less than the number of memory block of data deleted, Deposit new data.
For the choice of cache server, when there are more than one cache server, the algorithm used: first the key value of the hash operation, the results of the operation with the cache server data volume to take the remainder, is to be stored in the place.

Installation of Memcache

1.Memcached is a set of distributed cache system, need to download the installation package, run the corresponding service, in order to avoid each boot, you have to manually turn on memcache, so you need to install Memcache to the Windows service, set up automatic start.

(1) Open cmd window, run as administrator, navigate to the folder where the Memcached.exe is located (CD)

(2) Input installation instructions memcached.exe-d Install

(Uninstall command: ....) Uninstall Open command: Start restart command: Restart Close command stop)

(3) You can view services through the Microsoft interface by right-clicking on my Computer-admin-services and applications

(4) through Telnet (remote communication tool), test the communication with the memcached server, through the Control Panel programs and functions, the Telnet tool (just open the client is enough)

(5) through DOS command telnet 127.0.0.1 (Memcache's computer's IP) 11211 (memcache default port number), enter the Telnet client and Memcache server communication interface

(6) Input instruction stats can get some information of memcache, determine whether the installation is successful

Use of Memecache

1. After the installation is complete, then the Download the associated class library that is required for. NET to use Memcache Commons.dll,icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll

2. Create a new Console application

3. Test code

1 namespaceMemcachedemo2 {3     class Program4     {5         Static voidMain (string[] args)6{//distributed cluster settings, using the socket will use three handshake, if not changed server, it will be discarded7                string[] ServerList = {"127.0.0.1:11211","10.0.0.132:11211" };8  9              //Initialize PoolTenSockiopool pool =sockiopool.getinstance (); One pool. Setservers (serverlist); A   -Pool. Initconnections =3; -Pool. Minconnections =3; thePool. MaxConnections =5; -   -Pool. Socketconnecttimeout = +; -Pool. Sockettimeout = the; +   -Pool. Maintenancesleep = -; +Pool. Failover =true; A   atPool. Nagle =false; - pool. Initialize (); -   -              //get the client instance -memcachedclient MC =Newmemcachedclient (); -Mc. EnableCompression =false; in   -Console.WriteLine ("------------Test-----------"); toMc. Set ("Test","My value");//stores the data to the cache server, where the string "My value" is cached, and key is "test" +   -              if(MC. Keyexists ("Test"))//test cache has a project with key test the             { *Console.WriteLine ("test is Exists"); $Console.WriteLine (MC. Get ("Test"). ToString ());//get the item with key test in the cachePanax Notoginseng             } -              Else the             { +Console.WriteLine ("Test not Exists"); A             } the   + console.readline (); -  $Mc. Delete ("Test");//Remove the item in the cache with key test $   -              if(MC. Keyexists ("Test")) -             { theConsole.WriteLine ("test is Exists"); -Console.WriteLine (MC. Get ("Test"). ToString ());Wuyi             } the              Else -              { WuConsole.WriteLine ("Test not Exists"); -              } About console.readline (); $               -Sockiopool.getinstance (). Shutdown ();//Close the pool, close sockets -  -         } A     } +}

Package of Memcache

 Public  classMemcachehelper {Private StaticMemcachedclient MC;  Public StaticMemcachehelper () {//distributed cluster settings, using the socket will use three handshake, if not changed server, it will be discarded//this should be placed in the configuration file.         string[] ServerList = {"127.0.0.1:11211","10.0.0.132:11211" }; //Initialize PoolSockiopool pool =sockiopool.getinstance (); Pool.         Setservers (serverlist); Pool. Initconnections=3; Pool. Minconnections=3; Pool. MaxConnections=5; Pool. Socketconnecttimeout= +; Pool. Sockettimeout= the; Pool. Maintenancesleep= -; Pool. Failover=true; Pool. Nagle=false; Pool.         Initialize (); //get the client instanceMC =Newmemcachedclient (); Mc. EnableCompression=false; }      Public Static BOOLSet (stringKeyObjectvalue) {        returnMC.              Set (key, value); }      Public Static BOOLSet (stringKeyObjectvalue, DateTime datetime) {         returnMC.     Set (key, value, DateTime); }      Public Static ObjectGet (stringkey) {         returnMC.     Get (key); }      Public Static BOOLDelete (stringkey) {         returnMC.     Delete (key); }      Public Static BOOLADD (stringKeyObjectvalue) {         returnMC.     ADD (key, value); }      Public Static BOOLADD (stringKeyObjectvalue, DateTime datetime) {         returnMC.     ADD (key, value, DateTime); }    }

Use of memcached

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.