PHP distributed cache memcached familiarity and operations-PHP Tutorial

Source: Internet
Author: User
PHP distributed cache memcached familiarity and operations. In the era of the rise of the Internet, all major websites are faced with a big data flow problem. how can we speed up website access, reduces familiarity with and operations on distributed cache memcached for PHP databases

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

 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

 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:

 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.

 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"

In today's 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 access...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.