PHP's distributed cache memcached familiarity and operation _php Tutorials

Source: Internet
Author: User

PHP's distributed cache memcached familiar and operational


Nowadays the internet is rising, the major websites are faced with a big data flow problem, how to improve the website access speed, reduce the operation of the database; as a PHP developer, we can generally think of a method of page static processing, anti-theft chain, CDN content distribution accelerated access, MySQL database optimization index, Set up Apache server cluster (), there are now popular in various distributed cache technology: such as Memcached/redis;

1. What is memcached?

A.memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.

B.memcached key is usually a string, the value cannot be duplicated; value can be put into strings, arrays, values, objects, booleans, binary data, and picture video

C.memcached default service port is 11211

2.PHP using memcached steps

<1> Preparation: Download the memcached Service installation package: memcached-1.2.6-win32-bin.7z and the DLL library accessing the memcached service: Php_memcache.dll

Www.memcached.org (the official website does not go in, can download from other places)

<2> Unpack the Package memcached-1.2.6-win32-bin.7z (can extract the copy to the Web server sibling directory), and then operation CMD, go to the directory just extracted with the command installation: memcached.exe-d Install

<3> installed (to determine if the installation is complete can go to the list of services to see if there is a memcached service), and then cmd start with the command: memcached.exe-d start

Here's how:

<4> after starting the memcached service, put the downloaded Php_memcache.dll into the EXT directory under the Web server PHP5 directory

<5> in php.ini, load the extension library Php_memcache.dll, and then restart the Apache server

<6> began to practice, memcached mainly have crud operations (that is, create, read, update, delete value operations, specifically can consult the manual), below a simple set of values, and then read the value of the operation

A. Setting the value page

 
  Connect ("127.0.0.1")) {    echo "failed to connect memcache server!";} setting, ' MyWord ' parameter represents key, ' Hello World ' stands for stored value, memcache_compressed represents compressed content, 50 represents storage time, unit seconds if ($mem->set (' MyWord ', ' Hello World ', memcache_compressed,50) {    echo ' setting value succeeded! ";}? >

Note: If the value is stored in memory for more than 30 days, use a timestamp to set 100 days: such as time () +3600*24*100; set 0 means never expire


B. Read Value page

 
  Connect ("127.0.0.1")) {    echo "failed to connect memcache server!";} Read key MyWord value $value = $mem->get (' MyWord '), if (! $value) {    echo "read failed! ";} else{    echo "Read value =". $value;}

C. Examples of deletions and updates:

 
  Connect ("127.0.0.1")) {    echo "failed to connect memcache server!";} setting, ' MyWord ' parameter represents key, ' Hello World ' stands for stored value, memcache_compressed represents compressed content, 50 represents storage time, unit seconds if ($mem->set (' MyWord ', ' Hello World ', memcache_compressed,50) {    echo ' setting value succeeded! ";} Read key MyWord value $value = $mem->get (' MyWord '), if (! $value) {    echo "read failed! ";} else{    echo "Read value =". $value;} Update key value $mem->replace (' MyWord ', ' Hello everybody! '); $value = $mem->get (' MyWord '), if (! $value) {    echo "read failed! ";} else{    echo "Read value =". $value;} Delete key MyWord value $mem->delete (' MyWord '); $value = $mem->get (' MyWord '); if (! $value) {    echo "read failed! ";} else{    echo "Read value =". $value;} Close $mem->close ();  ? >

Note: There are many methods under the Mem object that can be read through the manual.

<7> multiple memcached server settings, in fact, a little bit more than a memcached server change, is to add multiple memcached server through the method Addserver added to the connection pool, so after Setup, CRUD operations, Internally, the appropriate algorithm is used to balance the corresponding server and perform the corresponding operation.

 
  Addserver (' 192.168.0.1 ', 11211); $mem->addserver (' 192.168.0.2 ', 11211); $mem->addserver (' 192.168.0.3 ', 11211); $mem->addserver (' 192.168.0.4 ', 11211);//Set, ' MyWord ' parameter represents key, ' Hello World ' stands for stored value, memcache_compressed represents compressed content, 50 represents storage time, unit seconds if ($mem- >set (' MyWord ', ' Hello World ', memcache_compressed,50)) {    echo ' setting value succeeded! ";} Read key MyWord value $value = $mem->get (' MyWord '), if (! $value) {    echo "read failed! ";} else{    echo "Read value =". $value;}? >

<8>memcache access is no user state, security needs to be considered, generally by placing in the intranet, and through the firewall to restrict the external network access to memcache port to achieve security

<9> by modifying php.ini, you can put the value of the session into the Memcache server

Session.save_handler = files changed to Session.save_handler = memcached

Session.save_path = "N; Mode;/path "changed to Session.save_path =" tcp://127.0.0.1:11211 "

http://www.bkjia.com/PHPjc/976801.html www.bkjia.com true http://www.bkjia.com/PHPjc/976801.html techarticle PHP Distributed cache memcached familiar with and operation of today's Internet rise era, the major websites are facing a big data flow problem, how to improve the speed of website access, reduce the database ...

  • 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.