Introduction to memcached

Memcached is a high-performance distributed memory cache server. The general purpose is to cache the database query results to reduce the database access times, so as to improve the speed and scalability of dynamic Web applications

Sex. It can handle any number of connections, using non-blocking network IO. Because it works by carving out a space in memory and creating a HashTable, Memcached manages the HashTable itself.

Installation of memcached (see “Installation of Memcache under Linux”)

The latest version is 1.4.0. You can download memcached-1[1].4.0.tar.gz directly from your official website. Yum install libevent libevent-devel: install libevent libevent-devel: install libevent libevent-devel: install libevent-devel

Unpack, compile and install memcached-1[1].4.0.tar.gz:

The tar – XZF memcached – 1.1.12. Tar. Gz

CD memcached – 1.1.12



make install

Run the memcached daemon

Running the memcached daemon is simple, with a single command line and no configuration files to modify (and no configuration files for you to modify) :

/usr/local/bin/memcached-d-m 128-l 192.168.x.y-p 11211-u WWW

Parameter explanation:

-d Run memcached as a daemon;

-m Sets the amount of memory memcached can use, in m;

-l sets the IP address to listen on. If it is native, this parameter can usually be unset. (Usually do not write)

-p sets the port on which to listen. The default is 11211, so you can not set this parameter.

-u Specifies the user, which is required if you are currently root.

Of course, there are other parameters you can use, as shown in man memcached.

How memcached works

First way is memcached daemon running on one or more servers, operation at any time to accept the client’s connection, the client can by all sorts of languages, are now known to the client API including Perl/PHP/Python/Ruby/Java/C # / C etc. After a client, such as PHP, connects to the memcached service, the next thing to do is to access objects. Each object to be accessed has a unique identifier key by which access is performed. Objects stored in memcached are actually stored in memory. It is not stored in a cache file, which is why memcached is so efficient and fast. Note that these objects are not persistent and the data inside them is lost when the service stops.

How does PHP work as a memcached client

There are two ways to make PHP a memcached client and call memcached services for object access operations.

PHP has an extension called memcache. For Linux, you need to compile and install memcache. For Windows, you need to remove the comment before php_memcache. DLL in php.ini to make it available.

Refer to part of Zhang Yan’s article on installing Nginx

Compile and install the PHP5 extension module

Download the tar ZXVF memcache — 2.2.5. TGZ < — — (this is a PHP extension package repository) CD memcache 2.2.5 / / usr/local/webserver/PHP/bin/phpize. / configure –with-php-config=/usr/local/webserver/php/bin/php-config make make install cd .. /

Add extension = “” to php.ini

Example (PHP Memcache) :


<? php

$mem = new Memcache;

$mem->connect(“192.168.x.y”, 11211)or die (“Could not connect”);

$mem->set(‘key’, ‘This is a test! ‘, 0, 60);

$val = $mem->get(‘key’);

echo $val;

? >


connect (” “, 12000); $mem->set(‘key1’, ‘This is first value’, 0, 60); $val = $mem->get(‘key1’); echo “Get key1 value: ” . $val .” “; $mem->replace(‘key1’, ‘This is replace value’, 0, 60); $mem->replace(‘key1’, ‘This is replace value’, 0, 60); $val = $mem->get(‘key1’); echo “Get key1 value: ” . $val . ” “; $arr = array(‘aaa’, ‘BBB ‘,’ CCC ‘, ‘DDD ‘); $mem->set(‘key2’, $arr, 0, 60); $val2 = $mem->get(‘key2’); echo “Get key2 value: “; print_r($val2); echo ” “; $mm-> delete(‘key1’); $mm-> delete(‘key1’); $val = $mem->get(‘key1’); echo “Get key1 value: ” . $val . ” “; $mem->flush(); $mem->flush(); $val2 = $mem->get(‘key2’); echo “Get key2 value: “; print_r($val2); echo ” “; $mem->close(); $mem->close(); ? >

If This works, the browser will output: Get key1 value: This is first value Get key1 value: This is replace value Get key2 value: Array ( [0] => aaa [1] => bbb [2] => ccc [3] => ddd ) Get key1 value: Get key2 value:

Program code analysis

$mem = new Memcache; $mem = new Memcache;

$mm-> connect(“”,12000); $mm-> connect(“”,12000);

The first parameter is the key of the data, which is used to locate the data. The second parameter is the content of the data, which is a string. The third parameter is a mark, which is usually set to 0 or Memcache_Compressed. The fourth parameter is the validity period of the data, which means that the data is valid within this time period. If it is set to 0, it will be valid forever. Here we set 60, which is the validity period of one minute: $mm-> set(‘ key1 ‘, ‘This is first value’, 0, 60); $mm-> set(‘ key1 ‘, ‘This is first value’, 0, 60);

$val = $mm-> get(‘ key1 ‘); $val = $mm-> get(‘ key1 ‘); echo “Get key1 value: ” . $val;

Now replace the value of key1 with the replace method. The arguments to the replace method are the same as the arguments to the set, but the first argument, key1, must be the key to replace the contents of the data. $mem->replace(‘ key1 ‘, ‘This is replace value’, 0, 60); $val = $mem – > get (” key1 ‘); echo “Get key1 value: ” . $val;

$arr = array(‘ aaa ‘, ‘BBB’, ‘CCC’, ‘DDD’); $arr = array(‘ aaa ‘, ‘BBB’, ‘CCC’, ‘DDD’); $mem – > set (” key2 ‘, $arr, 0, 60). $val2 = $mem – > get (” key2 ‘); print_r($val2);

$mm-> delete(‘ key1 ‘); $mm-> delete(‘ key1 ‘); $mm-> delete(‘ key1 ‘); $val = $mem – > get (” key1 ‘); echo “Get key1 value: ” . $val . ” “;

Finally, we will clean up all the data stored in the Memcache server, and find that the data is gone, and finally output the data of key2 is empty, and finally close the connection

(3) data

uid     username   password                         gender
751490 a62986233 8d03f56ad2c48d494cd4b73b3ba64dca       0
751489 hldhoxbh   25d55ad283aa400af464c76d713c07ad       0
751488 wuhan0088 ba2206cbae7c1bfe33a54ff161943bab       0
751487 anrron     b206e95a4384298962649e58dc7b39d4       0
751486 hldjxlkx   9c98df872d24244696c393a1d26ab749       0
751485 1394afjh   25d55ad283aa400af464c76d713c07ad       0
751484 yesi808   32baeaa3c422413843b015919c0be999       0
751483 IDC010pw   25f9e794323b453885f5181f1b624d0b       0
751482 ebay360v   a36b9e764318d31b4810d7d18096e6e7       0
751481 ppgqsvgv   9c98df872d24244696c393a1d26ab749       0

10 rows in set (0.00 sec)

<? php

# database connection information

$host = “”;




$query=”select UID,username,password,gender from pw_members order by uid desc limit 10;”

# I have used MD5 encryption as the key, or other encryption methods, such as Base64


$mem=new Memcache();

$mem – > connect (” “, 11211);

if(! $result=$mem->get($m_key)){

Echo “This is the result read from the database!” ;

$connection=mysql_connect($host,$user,$passwd) or die (“Unable to conect!” );


$result=mysql_query($query) or die(“Error query!” .mysql_error());









Echo “This is the result of reading from the Memcached Server! n”;



# Display the retrieved data

echo “<table cellpadding=10 border=2>”;

echo “<tr>”;

echo “<th>Uid</th>”;

echo “<th>Username</th>”;

echo “<th>PassWord</th>”;

echo “<th>Gender</th>”;

echo “</tr>”;

for($i=0; $i<10; $i++){

echo “<tr>”;

for($j=0; $j<4; $j++){

echo “<td>”.$result[$i][$j].”</td>”;


echo “</tr>”;


? >

The second option, which avoids the hassle of extending and recompiling, is to use php-memcached-client.php directly, but at a slightly less efficient rate than the extended library.

First, download memcached-client.php. Once you have downloaded memcached-client.php, you can operate on the memcached service using the class “memcached” in this file. The main methods used are add(), get(), replace(), and delete(). These methods are described as follows:

Add ($key, $val, $exp = 0) add ($key, $val, $exp = 0) add ($key, $val);

Get ($key) retrieves the object data from memcached, using the object’s unique identifier $key;

Replace ($key, $value, $exp=0); replace ($key, $value, $exp=0) with $value; replace ($key, $value, $exp=0) with $value;

Delete ($key, $time = 0) Deletes an object in memcached with an identifier of $key. $time is an optional parameter that indicates how long to wait before deleting.

Here is a simple test code that accessor or accessor the data of an object with the identifier ‘mykey’ :

<? php

// Include the memcached class file


// Options Settings

$options = array(

Servers: Servers => array(‘’), // Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers: Servers

‘debug’ => false; // Whether to turn on debug

‘compress_threshold’ => 10240, // how many bytes of data are exceeded when compressed

‘persistant’ => false // Use persistent connections


// Create an instance of the memcached object

$mc = new memcached($options);

// Set the unique identifier used by this script

$key = ‘mykey’;

// Write the object to memcached

$mc->add($key, ‘some random strings’);

$val = $mc->get($key);

echo “

“.str_pad(‘$mc->add() ‘, 60, ‘_’).”



// Replace the written object data value

$mc->replace($key, array(‘some’=>’haha’, ‘array’=>’xxx’));

$val = $mc->get($key);

echo “

“.str_pad(‘$mc->replace() ‘, 60, ‘_’).”



// Delete the object in memcached


$val = $mc->get($key);

echo “

“.str_pad(‘$mc->delete() ‘, 60, ‘_’).”



? >

In practice, you usually save the result set of a database query to memcached and retrieve it directly from memcached the next time you access it. This can reduce the load on the database to a large extent. The value after the SQL statement md5() is typically used as the unique identifier key. Here is an example of using memcached to cache the result set of a database query (this code snippet follows the above example code) :

<? php

$sql = ‘SELECT * FROM users’;

$key = md5($sql); // Memcached object identifier

if ( ! ($datas = $mc->get($key)) ) {

// No cached data is retrieved in memcached. Use a database query to retrieve the recordset.

echo “

“.str_pad(‘Read datas from MySQL.’, 60, ‘_’).”


$conn = mysql_connect(‘localhost’, ‘test’, ‘test’);


$result = mysql_query($sql);

while ($row = mysql_fetch_object($result))

$datas[] = $row;

// Save the result set data retrieved from the database to memcached for the next time you access it.

$mc->add($key, $datas);

} else {

echo “

“.str_pad(‘Read datas from memcached.’, 60, ‘




? >

As you can see, with memcached, you can reduce database connections, queries, load the database, and speed up your scripts.

Websites that use Memcache usually have large traffic. In order to relieve the pressure on the database, Memcache can be used as a cache area to store part of the information in the memory so that it can be accessed quickly in the front end. The general focus is on how to share the database load and distribute it, since the memory capacity of a single Memcache is limited. Here I simply put forward my personal views, without practice, as a reference.

Distributed application Memcache originally supports distributed, we have slightly modified the client, better support. Our keys can be properly encapsulated regularly. For example, in a User-based website, each user has a User ID, so extraction and access can be carried out according to a fixed ID. For example, users starting with 1 are stored in the first Memcache server. The data of users starting with 2 is stored in the second Mecache server, and the data access is first converted and accessed according to the User ID.

However, this has a disadvantage, that is, the User ID needs to be judged, if the business is inconsistent, or other types of applications, may not be so appropriate, then you can consider according to their actual business, or to think of a more appropriate method.

Reducing the pressure of the database This is more important, basically all the data are stored in the database of, each time the frequent access to the database, resulting in a decline in database performance extremely, unable to serve more users at the same time, such as MySQL, particularly frequent lock table, then let Memcache share the webmaster blog the pressure of the database. We need to make changes to the current architecture in a way that is relatively minor and does not involve major changes to the front end.

A simple approach I consider: The database operation module at the back end extracts all the SELECT operations (UPDATE/DELETE/INSERT regardless), and then calculates a hash data key from the corresponding SQL using the corresponding hash algorithm (such as MD5 or SHA). If the data does not exist, it has not been written to the cache. Then extract the data from the database, one is an array class, and set the data in Memcache, the key is the hash value of the SQL, and then set the corresponding invalidate time. For example, an hour, in which data is extracted from the cache, effectively reduces the pressure on the database. The disadvantage is that the data is not real-time. When the data is modified, it cannot be displayed at the front end in real time, and it may also take a large amount of memory. After all, the amount of data produced by each select may be huge, which is a factor to be considered.

The above server side of Memcache operates directly after connecting with the client side without any verification process. In this way, if the server is directly exposed on the Internet, it is quite dangerous. At worst, if the data is leaked and viewed by other irrelevant people, the server will be invaded. Because Mecache runs as root, and there may be some bugs or buffer overruns that we don’t know about, the danger is predictable. For the sake of security, I would like to make two suggestions, which can slightly prevent the intrusion of hackers or data leakage.

Intranet access It is best to have an Intranet access between two servers, usually between the Web server and the Memcache server. A common server has two network cards, one pointing to the Internet and the other pointing to the Intranet, so let the Web server access the Memcache server through the Intranet network card. When we start up the server of Memcache, we listen to the IP address and port of the Intranet. The access between the intranets can effectively prevent other illegal access. Memcached-d-m 1024-u Root-l 11211-c 1024-p/TMP / memcached-d-m 1024-u Root-l 11211-c 1024-p/TMP /memcached The Memcache server is set up to listen on port 11211 over IP on the Intranet, takes up 1024MB of memory, and allows up to 1024 concurrent connections

A firewall is a simple and effective way to set up a firewall. If both servers are connected to the network and need to access Memcache via an external IP, then you can consider using a firewall or proxy to filter out illegal access. In general, we can use iptables or ipfw under FreeBSD to specify some rules to prevent illegal access. For example, we can set only our Web server to access our Memcache server, and prevent others from accessing. Iptables -f iptables -p INPUT DROP iptables -a INPUT -p TCP -s — dport 11211-j ACCEPT iptables -a INPUT -p UDP-S — DPORT 11211-J Accept iptables rule: Web server is allowed to access Memcache server, which can effectively prevent some illegal access Corresponding rules can be added to enhance security, and this can be done according to your own needs.

Reference (1)

Memcache installation on Linux

Recently, we are working on how to make Discuz! Apply Memcache to do something, and document the installation.

Install the Memcache server side on Linux. The latest version is memcached-1.3.0. In addition, Memcache uses the libevent library for Socket handling, so you also need to install libevent. The latest version of libevent is libevent-1.3. (If you already have libevent installed on your system, you don’t need to.) Download both of these with the wget command. After downloading back to the source file. 1. Install libevent first. /configure — prefix=/usr; /configure — prefix=/usr; Then make; Then make install; /configure — with-libevent=/usr; /configure — with-libevent=/usr; /configure — with-libevent=/usr; Then make; Then make install; This completes the installation of Memcache server-side on Linux. The detailed method is as follows:

1. Download memcached and libevent respectively and place them in/TMP: CD/TMP 1.2.0.tar.gz

Libevent: tar ZXVF libevent-1.2.tar.gz CD libevent-1.2. /configure — prefix=/usr make make install

3. Test if libevent successful installation: ls – al/usr/lib | grep libevent LRWXRWXRWX 1 root root 21 11?? 12 17:38> -rwxr-xr-x 1 root root 263546 11?? 12 17:38 — r — 1 root root 454156 11?? 12 17:38 libevent.a -rwxr-xr-x 1 root root 811 11?? 12 17:38 lrwxrwxrwx 1 root root 21 11?? 12 17:38 -> Not bad, it’s all installed

4. To install memcached, specify the installation location of libevent: CD/TMP tar ZXVF memcached-1.2.0.tar.gz CD memcached-1.2.0. /configure — with-libevent=/usr make make install Please check the error message carefully and follow the error message to configure or add the corresponding library or path. Install memcached in /usr/local/bin/memcached

/usr/local/bin/mem* -rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached -rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug

1. Select the version of Memcache that you want to download. 2. Install PHP’s Memcache extension

The tar VXZF memcache – 2.2.1. Memcache – 2.2.1 TGZ CD/usr/local/PHP/bin/phpize. / configure — enable – memcache — with-php-config=/usr/local/ PHP /bin/php-config — with-zlib-dir make make make install — with-zlib-dir make make install

3. After the above installation, there will be a prompt like this:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/

4. Change extension_dir = “./ “from php.ini to”

Extension_dir = “/ usr/local/PHP/lib/PHP/extensions/no – debug – non – ZTS – 2007 – XXXX/”

5. Add a line to load the memcache extension:

1. Start the server side of Memcache: /usr/local/bin/ memcached-d-m 10-u root-l 120-c 256-p/TMP /

-d is a daemon, -m is the amount of memory allocated to Memcache in MB, -u is the user running Memcache, -l is the IP address of the server being listened on. If there are multiple addresses, -m is the amount of memory allocated to Memcache in MB. IP address of the server is specified here. -p is the port for Memcache listening, and it is better to set the port above 1024. -c is the maximum number of concurrent connections running, and the default is 1024. / TMP/ = / TMP/ = / TMP/ = / TMP/ = / TMP/ = / TMP/ = / TMP/ = / TMP/

2. To terminate the Memcache process, execute:

kill cat /tmp/

Multiple daemons can also be started, but the ports cannot be duplicated.

Service Httpd Restart

Memcache environment test: Run the following PHP file, if there is an output of This is a test! , indicates that the environment is successfully set up. Get started with Memcache!
connect (” “, 11211); $mem->set(‘ key ‘, ‘This is a test! , ‘0, 60); $val = $mem – > get (” key “); echo $val; ? >