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 there are 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.
The specific configuration You can refer to:
Memcache installation under Linux: http://www.ccvita.com/257.html
Memcache installation under Windows: http://www.ccvita.com/258.html
Memcache Basic Tutorial: http://www.ccvita.com/259.html
discuz! memcache Cache implementation: http://www.ccvita.com/261.html
Memcache Agreement Chinese version: http://www.ccvita.com/306.html
Memcache distributed Deployment Scenarios: http://www.ccvita.com/395.html
PHP's Memcache
< 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 ('