This article describes how to install and use memcache in the php environment. It can apply any number of connections and use non-blocking network I/O. Because its working mechanism is to open up a space in the memory, and then establish a HashTable, Memcached self-manages these HashTable, interested friends to learn it together, Memcache is a project of danga.com, the earliest service was LiveJournal. Many people around the world use this cache project to build their own websites with high loads to share the pressure on databases. It can cope with any number of connections and use non-blocking network I/O. Because its working mechanism is to open up a space in the memory, and then create a HashTable, Memcached self-manages these HashTable. Official website of Memcache: http://www.danga.com/memcached. you can find more detailed information here.
Why are there two Memcache and memcached names? In fact, Memcache is the name of this project, and memcached is the name of the main program file on its server ~~~~. One is the project name, the other is the main program file name, and many people on the Internet do not understand it, so it is mixed.
Install Memcache
There are two steps: Install the memcache server and the memcached client.
The so-called Server installation is to install Memcache on the server (generally in linux) to store data.
The so-called Client installation refers to php (or other programs, Memcache and other good api interfaces) to use the functions provided by Memcache on the server side. php needs to add extensions.
PHP Memcache
<? Php // connect $ mem = new Memcache; $ mem-> connect ("db.nowamagic.net",); // save data $ mem-> set ('key ', 'This is first value',); $ val = $ mem-> get ('key'); echo "Get key value :". $ val."
"; // Replace Data $ mem-> replace ('key', 'this is replace value',); $ val = $ mem-> get ('key '); echo "Get key value :". $ val."
"; // Save the array $ arr = array ('aaa', 'bbb ', 'ccc', 'ddd '); $ mem-> set ('key ', $ arr,); $ val = $ mem-> get ('key'); echo "Get key value:"; print_r ($ val); echo"
"; // Delete data $ mem-> delete ('key'); $ val = $ mem-> get ('key'); echo" Get key value :". $ val."
"; // Clear all data $ mem-> flush (); $ val = $ mem-> get ('key'); echo" Get key value :"; print_r ($ val); echo"
"; // Close the connection $ mem-> close ();?>
If it is normal, the browser will output:
Get key value: This is first value
Get key value: This is replace value
Get key value: Array ([] => aaa [] => bbb [] => ccc [] => ddd)
Get key value:
Get key value:
Program code analysis
Initialize a Memcache object: $ mem = new Memcache;
Connect to our Memcache server. The first parameter is the Server IP address or host name, and the second parameter is the open port of Memcache: $ mem-> connect ("192.168.0.200 ", 12000 );
Save a data to the Memcache server. The first parameter is the data key used to locate a data. The second parameter is the data content to be saved. Here is a string, the third parameter is a tag, which is usually set to 0 or MEMCACHE_COMPRESSED. The fourth parameter is the data validity period, which means that the data is valid during this time, the data will be cleared by the Memcache server in seconds. If it is set to 0, it will always be valid. Here we set 60, which is the one-minute validity period: $ mem-> set ('key1', 'this is first value', 0, 60 );
Get a piece of data from the Memcache server. It has only one parameter, that is, the key to get the data. Here is the key1 set in the previous step. Now we get the data and output it:
$val = $mem->get('key′);echo "Get key value: " . $val;
The replace method is used to replace the above key1 value. The parameters of the replace method are the same as those of the set method. However, the first parameter key1 must be the key to replace the data content, the output is as follows:
$mem->replace('key', 'This is replace value', , );$val = $mem->get('key');echo "Get key value: " . $val;
Similarly, Memcache can store arrays. Below is an array saved on Memcache, which is then retrieved and output:
$arr = array('aaa', 'bbb', 'ccc', 'ddd');$mem->set('key', $arr, , );$val = $mem->get('key');print_r($val);
Now, you can delete a data file by using the delte interface. The parameter is a key, and the data of the key of the Memcache server can be deleted. No result is returned when the output is complete:
$mem->delete('key');$val = $mem->get('key');echo "Get key value: " . $val . "
";
Finally, we clear all the data stored on the Memcache server, and we will find that no data exists. Finally, the output key2 data is empty, and the connection is closed:
$mem->flush();$val = $mem->get('key');echo "Get key value: ";print_r($val);echo "
";
Use of Memcache
Memcache websites generally have a large traffic volume. To relieve the pressure on the database, apsaradb for Memcache is used as a cache area and some information is stored in the memory, allowing quick access at the front end. The general focus is to focus on how to share the pressure on the database and distribute it. After all, the memory capacity of a single Memcache is limited. I would like to put forward my personal opinion here. Without practice, you can use it as a reference.
Distributed Application
Apsaradb for Memcache originally supports distributed architectures. Our client has been slightly transformed to provide better support. Our keys can be properly encapsulated. For example, for a user-based website, each User has a user ID, which can be extracted and accessed based on a fixed ID, for example, users starting with 1 are saved on the first Memcache server, and user data starting with 2 is stored on the second child Mecache server, data access is converted and accessed according to the User ID.
However, this disadvantage is that you need to determine the User ID. if the business is inconsistent or other types of applications are not suitable, you can consider it based on your actual business, or think about a more appropriate method.
Reduce database pressure
This is important. All the data is basically stored in the database. Each time the database is accessed frequently, the database performance is greatly reduced and it cannot serve more users at the same time, for example, for MySQL, tables with frequent locks, let Memcache share the pressure on the database. We need a method that is relatively small and will not change the front-end on a large scale to change the current architecture.
A simple method I want to consider:
The back-end database operation module extracts all Select operations (update/delete/insert ), then, calculate the corresponding SQL statement using the corresponding hash algorithm to obtain a hash data key (such as MD5 or SHA), and then query the key in Memcache for data. If the data does not exist, it indicates that the data has not been written into the cache, so the data is extracted from the database. One is the array format, and the data is then set to Memcache. The key is the hash value of this SQL statement, then set an expiration time, for example, one hour. The data in one hour is extracted from the cache, effectively reducing the pressure on the database. The disadvantage is that the data is not real-time. After the data is modified, it cannot be displayed on the front-end in real time, and memory usage may be high. After all, the number of data in each select operation may be large, this is a factor to consider.
Memcache Security
The above Memcache servers are directly connected through the client and there is no verification process. In this way, it is dangerous to directly expose the server to the Internet, if data leaks are viewed by other unrelated personnel, the server is infiltrated because Mecache runs with the root permission. Besides, some unknown bugs or buffer overflow may exist, these are all unknown, so the danger is foreseeable. For the sake of security, I have made two suggestions to slightly prevent hacker intrusion or data leakage.
Intranet access
It is recommended that the access between the two servers is in the Intranet format, generally between the Web server and the Memcache server. Generally, the server has two NICs, one pointing to the Internet and the other pointing to the Intranet, so that the Web server can access the Memcache server through the Intranet Nic, when the Memcache server is started, it listens to the Intranet IP address and port, and the 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
The Memcache server monitors port 11211 of the ip address 192.168.0.200 over the Intranet, occupying 1024 MB of memory and allowing a maximum of concurrent connections.
Set firewall
Firewall is a simple and effective method. If both servers are connected to the Internet and Memcache needs to be accessed through an Internet IP address, you can use a firewall or proxy program to filter out illegal access. In Linux, we can use iptables or FreeBSD ipfw to specify rules to prevent unauthorized access. For example, we can set to allow only our Web servers to access our Memcache server, at the same time, other accesses are blocked.
# Iptables-F
# Iptables-P INPUT DROP
# Iptables-a input-p tcp-s...-dport-j ACCEPT
# Iptables-a input-p udp-s...-dport-j ACCEPT
The above iptables rule only allows access from the Web server 192.168.0.2 to the Memcache server. It can effectively prevent some illegal access and add other rules to enhance security, this can be done according to your own needs.