What's memcache?
Memcache is a danga.com project, the first to serve the LiveJournal, many people around the world use this cache project to build their own heavy load of the site to share the pressure of the database.
It can handle any number of connections and use non-blocking network IO. Because its working mechanism is to open up a space in memory, and then establish a hashtable,memcached self management of these hashtable.
Memcache Official website: http://www.danga.com/memcached, more detailed information can come here to understand
Why are there memcache and memcached two kinds of names?
In fact Memcache is the name of this project, and memcached is its server-side main program file name, know what I mean put ~ ~ ~ ~ ~ One is the project name, one is the main program file name, on the internet to see a lot of people do not understand, so mixed use.
installation of Memcache
Divided into two procedures: Memcache server-side installation and memcached client installation.
The so-called server-side installation is on the server (typically Linux system) on the installation of Memcache implementation data storage
The so-called client installation means PHP (or other programs, memcache and other good API interface) to use the server-side memcache provided functions, the need for PHP to add extensions.
PHP's Memcache
Copy Code code as follows:
< PHP
Connection
$mem = new Memcache;
$mem->connect ("192.168.0.200", 12000);
Save data
$mem->set (' key1 ', ' This is ', ' 0, 60 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. " <br/> ";
Replace data
$mem->replace (' key1 ', ' This is replace value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. "<br/>";
Save Array
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0, 60);
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br/>";
Delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. "<br/>";
Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br/>";
Close connection
$mem->close ();
?>
If normal, the browser will output:
Get Key1 Value:this is a
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
Initializes an Memcache object:
$mem = new Memcache;
Connected to our Memcache server, the first parameter is the IP address of the server, or the hostname, and the second parameter is the open port of the memcache:
$mem->connect ("192.168.0.200", 12000);
Save a data to the Memcache server, the first parameter is the key of the data, used to locate a data, the second parameter is to save the data content, here is a string, the third parameter is a tag, generally set to 0 or memcache_compressed on the line, The fourth parameter is the validity of the data, that is, the data in this time is valid, if the past this time, then will be Memcache server to clear the data, unit is seconds, if set to 0, it is always valid, we set 60 here, is a minute effective time:
$mem->set (' key1 ', ' This is ', ' 0, 60 ');
From the Memcache server to get a data, it has only one parameter, is the need to obtain data key, we here is the previous step set Key1, now get this data output output:
$val = $mem->get (' key1′);
echo "Get Key1 Value:". $val;
Instead of replacing the Key1 value with the Replace method, the parameters of the Replace method are the same as set, but the first parameter key1 must be the key to replace the data content, and the last output is:
$mem->replace (' key1′, ' This is replace value ', 0, 60);
$val = $mem->get (' key1′);
echo "Get Key1 Value:". $val;
Similarly, memcache can also save an array, and the following is an array stored on the memcache and then fetched back and output
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' key2′, $arr, 0, 60);
$val 2 = $mem->get (' key2′);
Print_r ($val 2);
Now deletes a data, uses the Delte interface, the parameter is a key, then can delete the Memcache server This key data, the final output time does not have the result
$mem->delete (' key1′);
$val = $mem->get (' key1′);
echo "Get Key1 Value:". $val. "<br>";
Finally, we have all the data saved on the Memcache server cleared, will find that the data is not, the final output key2 data is empty, and finally close the connection
$mem->flush ();
$val 2 = $mem->get (' key2′);
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
the use of memcache
Use Memcache Web site general traffic is relatively large, in order to ease the pressure of the database, so that memcache as a cache area, the part of the information stored in memory, the front-end can be accessed quickly. So the general focus is on how to share the database pressure and distributed, after all, the single memcache memory capacity of the limited. I am here to briefly present my personal views, without practice, right as a reference.
Distributed Applications
Memcache originally supported the distributed, our client slightly modified, better support. Our key can be properly packaged in a regular way, for example, user-oriented Web sites, each users have user ID, you can follow a fixed ID for extraction and access, such as 1 of users at the beginning of the first memcache server, The data of the user starting with 2 is stored on the second Mecache server, and the access data is converted and accessed according to the user ID first.
But the disadvantage is that you need to judge the user ID, if the business is inconsistent, or other types of applications may not be so appropriate, then you can consider the actual business, or think of a more appropriate method.
Reduce database pressure
This is more important, all the data are basically kept in the database, every time the frequent access to the database, resulting in a very degraded database performance, can not serve more users at the same time, such as MySQL, particularly frequent lock table, so let memcache to share the pressure of the database. We need a way to change the current architecture by making small changes that will not change the front end on a large scale.
One simple way I consider:
The back-End Database operation module, which extracts all select operations (Update/delete/insert), and then calculates the corresponding hash algorithm for the corresponding SQL to calculate a hash data key (such as MD5 or SHA), Then the key to Memcache to find data, if this data does not exist, the description has not been written to the cache, then from the database to extract the data, one is the array class format, and then the data in set to Memcache, key is the hash value of this SQL, Then the corresponding setting of a failure time, such as one hours, then one hours of data are extracted from the cache, effectively reduce the pressure on the database. The disadvantage is that the data is not real-time, when the data has been modified, can not be real-time to the front end display, and there may be a larger memory footprint, after all, the number of select data may be large, this is a factor to consider.
the safety of Memcache
Our above Memcache server side is directly through the client connection directly after the operation, without any verification process, so if the server is directly exposed to the Internet is more dangerous, light data leakage by other unrelated personnel to view, heavy server was invaded, Because the Mecache is run as root, and there may be some unknown bugs or buffer overflows, these are unknown, so the danger is predictable. To be on the safe side, I make two suggestions that can be a little bit of protection against hacking or data leaks.
Intranet Access
It is best to access the two servers in the intranet form, typically between the Web server and the Memcache server. Universal servers are two network cards, a point to the Internet, a point to the intranet, then let the Web server through the intranet network card to access the Memcache server, we memcache the server on the start of the monitoring intranet IP address and port, Access between the intranet can effectively prevent other illegal access.
# memcached-d-M 1024-u root-l 192.168.0.200-p 11211-c 1024-p/tmp/memcached.pid
Memcache server-side setup listens to 11211 ports of 192.168.0.200 IP through the intranet, consumes 1024MB of RAM, and allows maximum 1024 concurrent connections
Setting up Firewalls
Firewall is a simple and effective way, if it is two servers are hanging in the net, and need to access memcache through the extranet IP, then you can consider using a firewall or agent program to filter illegal access.
In general, we can use Iptables or FreeBSD under Linux to specify some rules to prevent illegal access, such as we can set up only our web server to access our Memcache server, while blocking other access.
# iptables-f
# iptables-p INPUT DROP
# iptables-a input-p tcp-s 192.168.0.2–dport 11211-j ACCEPT
# iptables-a input-p udp-s 192.168.0.2–dport 11211-j ACCEPT
The iptables rule above is to allow only 192.168.0.2 this Web server access to the Memcache server, can effectively prevent some illegal access, the corresponding can also add some other rules to enhance security, this can be done according to their own needs.