PHP distributed cache memcached familiarity and operations, php cache memcached
In the era of the rise of the Internet, all major websites are faced with a big data stream problem. How can we increase website access speeds and reduce database operations? As a PHP developer, we can generally think of static page processing, anti-leeching, accelerated access to CDN content distribution, index creation for mysql database optimization, and apache server cluster creation () also, various popular distributed cache technologies, such as memcached/redis;
1. What is Memcached?
A. Memcached is a high-performance distributed memory object Cache System for dynamic Web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and database-driven websites. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol.
B. The key of Memcached is generally a string, which cannot be repeated. values can be placed into strings, arrays, values, objects, Boolean values, binary data, and image videos.
C. The default Memcached service port is 11211.
2. Steps for using Memcached in PHP
<1> preparation: Download The Memcached service installation package: memcached-1.2.6-win32-bin.7z and access the Memcached service dll library: php_memcache.dll
Www.memcached.org)
<2> decompress the package memcached-1.2.6-win32-bin.7z (you can decompress the package and copy it to the same Directory on the web server). Then, run cmd To Go To The extracted directory and run the command to install memcached.exe-d install.
<3> after the installation is complete (you can go to the service list to check whether the memcached service exists), run the command "memcached.exe-d start" in cmd to start memcached.exe
The procedure is as follows:
<4> after starting the memcached service, place the downloaded php_memcache.dll to the ext directory under the php5 directory of the web server.
<5> modify the file in php. ini, load the extension library php_memcache.dll, and restart the apache server.
<6> in practice, memcached mainly involves crud operations (create, read, update, and delete value operations. For details, refer to the Manual). Below is a simple setting value, then, read the value.
A. Set Value page
<? Phpheader ("Content-type: text/html; charset = UTF-8"); // create a Memcache object $ mem = new Memcache (); // connect to the Memcache server if (! $ Mem-> connect ("127.0.0.1") {echo "failed to connect to the Memcache server! ";} // Set. The 'myword' parameter indicates the key, 'Hello world' indicates the stored value, MEMCACHE_COMPRESSED indicates the compressed content, and 50 indicates the storage time, unit: Second if ($ mem-> set ('myword', 'Hello world', MEMCACHE_COMPRESSED, 50) {echo "the value is set successfully! ";}?>
Note: If the value is stored in the memory for more than 30 days, set the time stamp to 100 days, for example, time () + 3600*24*100. If the value is set to 0, the value will never expire.
B. Read the value page
<? Phpheader ("Content-type: text/html; charset = UTF-8"); $ mem = new Memcache (); if (! $ Mem-> connect ("127.0.0.1") {echo "failed to connect to the Memcache server! ";}// Read key myword value $ value = $ mem-> get ('myword'); if (! $ Value) {echo "reading failed! ";}Else {echo" read value = ". $ value ;}
C. Example of deletion and update:
<? Phpheader ("Content-type: text/html; charset = UTF-8"); // create a Memcache object $ mem = new Memcache (); // connect to the Memcache server if (! $ Mem-> connect ("127.0.0.1") {echo "failed to connect to the Memcache server! ";} // Set. The 'myword' parameter indicates the key, 'Hello world' indicates the stored value, MEMCACHE_COMPRESSED indicates the compressed content, and 50 indicates the storage time, unit: Second if ($ mem-> set ('myword', 'Hello world', MEMCACHE_COMPRESSED, 50) {echo "the value is set successfully! ";}// Read key myword value $ value = $ mem-> get ('myword'); if (! $ Value) {echo "reading failed! ";}Else {echo" read value = ". $ value;} // update the key value $ mem-> replace ('myword', 'Hello everybody! '); $ Value = $ mem-> get ('myword'); if (! $ Value) {echo "reading failed! ";} Else {echo" read value = ". $ value;} // delete key myword value $ mem-> delete ('myword'); $ value = $ mem-> get ('myword'); if (! $ Value) {echo "reading failed! ";}Else {echo" read value = ". $ value ;}// close $ mem-> close () ;?>
Note: there are still many methods under the mem object. You can refer to the Manual for details.
<7> the configuration of multiple memcached servers is actually a little different than that of one memcached server. It is to add multiple memcached servers to the connection pool through addserver, during the crud operation, the corresponding server is evenly connected through the corresponding algorithms and the corresponding operations are performed.
<? Phpheader ("Content-type: text/html; charset = UTF-8"); // create a Memcache object $ mem = new Memcache (); // Add multiple memcached servers $ mem-> addserver ('2017. 168.0.1 ', 11211); $ mem-> addserver ('2017. 168.0.2 ', 11211); $ mem-> addserver ('2017. 168.0.3 ', 11211); $ mem-> addserver ('2017. 168.0.4 ', 11211); // set, the 'myword' parameter indicates the key, 'Hello world' indicates the stored value, MEMCACHE_COMPRESSED indicates the compressed content, and 50 indicates the storage time, unit: Second if ($ mem-> set ('myword', 'Hello world', MEMCACHE_COMPRESSED, 50) {echo "The value is set successfully! ";}// Read key myword value $ value = $ mem-> get ('myword'); if (! $ Value) {echo "reading failed! ";}Else {echo" read value = ". $ value ;}?>
<8> access to memcache is non-user. Security needs to be considered. Generally, memcache is stored on the Intranet and the firewall restricts access to the memcache port through the Internet.
<9> modify php. ini to put the session value on the memcache server.
Session. save_handler = files is changed to session. save_handler = memcached
Session. save_path = "N; MODE;/path" to session. save_path = "tcp: // 127.0.0.1: 11211"