First, Memcache application of a PHP and Memcache
1. PHP's Memcache Client
PHP has two memcache clients: PHP memcache and PHP memcached. PHP memcache independent PHP Implementation, is the old client, from our practice has been found to have a number of problems, and fewer features, properties can be set less; PHP memcached is based on the native C libmemcached extension, more perfect, proposed to replace the PHP Memcached.
2, PHP memcache problems
(1). Distributed issues
PHP memcache automatically switches instances by default, so it sometimes takes old data and value is erratic
(2). Stability problems under high concurrency
PHP memcache replaced by PHP memcached, under high concurrency stability greatly improved;
(3). Second time-out interval cannot modify the problem
PHP Memcache Client has a 1-second timeout interval cannot modify the problem: bool Memcache::connect (string $host [, int $port [, int $timeout]]) The third parameter could have set timeout, in seconds , but cannot be modified.
The following three methods for modifying the timeout are not valid:
With Memcache API Memcache::setserverparams cannot be modified;
Change Memcache source code VI php_memcache.h macro definition cannot be modified;
PHP.ini in this configuration: Default_socket_timeout = 60 is invalid for this timeout.
(4). Memcache and memcached contrast
Note, PHP memcache This old client in the property settings can be set very little, error code granularity is very coarse, error after difficult to locate, and lack of some functions, such as:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/AF/wKioL1YEBgPD_QMfAAF5pLoryDw125.jpg "title=" 6.png " alt= "Wkiol1yebgpd_qmfaaf5plorydw125.jpg"/>
Note, we use PHP memcached to synthesize the above suggestions. For more information, please refer to the official text.
3, PHP installation memcached extension
[Email protected] ~]# yum-y install gcc+ gcc-c++
To resolve memcached dependencies need to install libmemcached, note that it is best to install the 1.0.16 version, I tried the 17, 18 version has been unable to install:
[Email protected] ~]# TAR-XF libmemcached-1.0.16.tar.gz [[email protected] ~]# CD Libmemcached-1.0.16[[email protected] libmemcached-1.0.16]# Mkdir/usr/local/libmemcached[[email protected] libmemcached-1.0.16]#./configure-prefix=/usr /local/libmemcached--with-memcached [[email protected] libmemcached-1.0.16]# Make[[email protected] libmemcached-1.0.16]# make Install
To install the PHP memcached extension:
[[email protected] ~]# tar -xf Memcached-2.2.0.tgz [[email protected] ~]# cd memcached-2.2.0[[email protected] memcached-2.2.0]# /usr/local/php/bin/phpize configuring for:php api version: 20131106Zend Module Api No: 20131226Zend Extension Api No: 220131226[[email protected] memcached-2.2.0]# ./configure --enable-memcached --with-php-config=/usr/ local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl[[ Email protected] memcached-2.2.0]# make && make installinstalling shared extensions: /usr/local/php/lib/php/extensions/ no-debug-non-zts-20131226/
To add a configuration file:
[Email protected] ~]# vim/etc/php.d/memcached.iniextension = "/usr/local/php/lib/php/extensions/ No-debug-non-zts-20131226/memcached.so "
To add a test page:
[Email protected] ~]# cat/www/a.com/index.php<?php phpinfo ();? >
Browser testing:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/B2/wKiom1YEBmXzc_cQAANmNOCdtoA517.jpg "title=" 7.png " alt= "Wkiom1yebmxzc_cqaanmnocdtoa517.jpg"/>
4, PHP installation memcache extension
PECL Introduction
The full name of the PECL is the PHP Extension Community Library, which is open and packaged through PEAR (PHP Extension and application repository,php extension and application warehouse) packaging format Install the PHP extension repository warehouse. The PECL module can be downloaded and installed through the installation management of the PEAR Package Manager. Unlike most PEAR packages, the PECL extension contains C-language code that can be compiled into PHP Core, so you can compile the PECL extension library into a dynamically loaded. So shared library, or a method that compiles in a static manner with the PHP source code as a whole. The PECL extension Library contains many PHP extensions for XML parsing, database access, message parsing, embedded Perl, and Pthyon script interpreter, so in a sense, the operational efficiency is PECL higher than the previous PEAR expansion libraries.
PECL Common options:
To install the Memcache module using PECL:
[Email protected] ~]#/usr/local/php/bin/pecl install memcachebuild process completed successfullyinstalling '/usr/ Local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so ' Install ok:channel://pecl.php.net/ Memcache-2.2.7configuration option "Php_ini" is not set to php.ini locationyou should add "extension=memcache.so" to PHP.I Ni
Next we will add the PHP memcache configuration file:
[Email protected] ~]# vim/etc/php.d/memcache.iniextension = "memcache.so" [[email protected] ~]# service PHP-FPM restart Gracefully shutting down php-fpm. Donestarting PHP-FPM Done
Let's take a look at the PHP module:
[Email protected] ~]#/usr/local/php/bin/php-m |grep memcachememcachememcached
Browser testing:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/AF/wKioL1YEBouTlxsLAAHXFTrSbFU700.jpg "title=" 8.png " alt= "Wkiol1yeboutlxslaahxftrsbfu700.jpg"/>
To add a test script for memcached:
[[email protected] a.com]# cat memcached.php<?php $mem = new Memcache; $mem->connect ("192.168.1.6", 11211) or die ("Could not connect "), $version = $mem->getversion ();echo " Server ' s version: ". $ Version. " <br/>\n "; $mem->set (' Hellokey ', ' Hello world ', 0, 600) or die (" Failed to save data at the memcached server ");echo " Store data in the cache (data will expire in 600 seconds) <br/>\n "; $get _result = $mem->get (' Hellokey ');echo "$get _result is from memcached server."; ?>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/B2/wKiom1YEBrHA9D0YAACZfc-U4-U324.jpg "title=" 9.png " alt= "Wkiom1yebrha9d0yaaczfc-u4-u324.jpg"/>
[[Email protected] ~]# telnet 192.168.1.6 11211Trying 192.168.1.6...Connected to 192.168.1.6.Escape character is ' ^] '. Get Hellokeyvalue Hellokey 0 11Hello worldend
Second, Memcache application two nginx and Memcache
1. Memcached_module Module
Nginx Memcached_module module can read the content directly from the memcached server output, subsequent requests are no longer processed by the application, such as PHP-FPM, Django, greatly improve the speed of dynamic pages. Nginx is only responsible for reading data from the memcached server, to write data to memcached also need the background of the application to complete, the active cache of the page cache to memcached, can be redirected to the backend to deal with 404. Ngx_http_memcached_module can operate any software that concurrently uses the Memcached protocol. such as Ttserver, Membase and so on.
The structure diagram is as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/AF/wKioL1YEBuSQWAkLAADHOkvl_TM936.jpg "title=" 10.png "alt=" Wkiol1yebusqwaklaadhokvl_tm936.jpg "/>
memcached keys can be set by Memcached_key variables, such as $uri. If hit, then the direct output of the content, no hit means that Nginx needs to request from the application page. At the same time, we also want the application to write key-value pairs to memcached so that the next request can be obtained directly from memcached. If the key value does not exist, Nginx will report a not found error. The best approach is to use Error_page to specify and location request processing. Also includes "Bad Gateway" error and "Gateway Timeout" error, such as: Error_page 404 502 504 = @app;. Note: You need to set default_type, otherwise it may not appear normal.
2. Module description
Syntax: Memcached_bind Address | Off
Default value: None
Configuration segment: HTTP, server, location
Syntax: Memcached_buffer_size size;
Default value: 4k|8k;
Configuration segment: HTTP, server, location
Syntax: Memcached_connect_timeout time;
Default value: 60s;
Configuration segment: HTTP, server, location
Syntax: Memcached_gzip_flag flag;
Default value: None
Configuration segment: HTTP, server, location
Syntax: Memcached_next_upstream Error | Timeout | Invalid_response | Not_found | Off ...;
Default value: Error timeout;
Configuration segment: HTTP, server, location
Syntax: Memcached_pass address:port or socket;
Default value: None
Configuration segment: location, if in location
Syntax: Memcached_read_timeout time;
Default value: 60s;
Configuration segment: HTTP, server, location
Syntax: memcached_send_timeout
Default value: 60s
Configuration segment: HTTP, server, location
Usage: $memcached _key variable: The value of memcached key.
3, Nginx memcached enhanced version Ngx_http_enhanced_memcached_module based on the Nginx memcached module, the addition of new features are:
-
Custom HTTP headers, such as Content-type, last-modified.
-
hash key can be more than 250 characters and memcached restricted.
-
-
Deletes data from memcached over an HTTP request.
-
Clears all memcached cache data over HTTP requests.
-
get memcached state data over HTTP requests.
-
Key name space management, to partially flush the cache.
-
cache through If-modified-since header and content last-modified back and forth Complex 304Not modified request.
server { listen 80; server_name www.test.com ; #charset koi8-r; #access_log logs/host.access.log main; location / { set $memcached _key $uri; memcached_pass 192.168.1.6:11211; default_type text/html; error_page 404 @fallback; } location @fallback { proxy_pass http://192.168.1.9; } }
From the above configuration file we can see that a request arrives, its URI as key to go to memcached server 192.168.1.6:11211 to find value, if there is no hit, then return 404. At this point the 404 receive is transferred to @fallback via Error_page and returned to the Web service to process the request.
Three, Memcache application three session and Memcache
1. Configure Node1 and Node2 to install PHP memcache extensions
Note that the above has demonstrated how to install the PHP memcache extension, which is no longer demonstrated here.
2. Install Memcache Server
Note, the same as everyone to install their own.
3. Modify the PHP configuration file
[Email protected] ~]# Vim/etc/php.inisession.save_handler = Memcachesession.save_path = "tcp://192.168.1.6:11211?" Persistent=1&weight=1&timeout=1&retry_interval=15 "
4. Add Test page
New PHP page setsess.php, enabling session for client settings.
<?phpsession_start (); if (!isset ($_session[' www.example.com ')) {$_session[' www.example.com '] = time (); } print $_session[' www.example.com '; print "<br><br>"; Print "Session ID:". session_id (); ?>
Description: New PHP page showsess.php, gets the session ID of the current user.
<?phpsession_start (); $memcache _obj = new Memcache; $memcache _obj->connect (' 192.168.1.6 ', 11211); $mysess =session_id (); Var_dump ($memcache _obj->get ($mysess)); $memcache _obj->close (); ?>
Iv. Memcache Graphical management tools
1, decompression Memadmin
[Email protected] ~]# TAR-XF memadmin-1.0.12.tar.gz [[email protected] ~]# CD Memadmin
2, Mobile memadmin to the page directory
[Email protected] memadmin]# mv. /memadmin/www/a.com/[[email protected] memadmin]# cd/www/a.com/
3. Check the configuration file
[[email protected] a.com]# vim memadmin/config.php$config[' user '] = "admin"; Your username$config[' passwd '] = "admin"; Your password
The default username is admin, password is admin.
4. Browser Testing
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/AF/wKioL1YEBzOAWJpNAACRsAAgRN8445.jpg "title=" 11.png "alt=" Wkiol1yebzoawjpnaacrsaagrn8445.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/AF/wKioL1YECKXB_Qj8AAQugkzMG6w949.jpg "title=" 12.png "alt=" Wkiol1yeckxb_qj8aaqugkzmg6w949.jpg "/>
This article is from the "Bread" blog, make sure to keep this source http://cuchadanfan.blog.51cto.com/9940284/1697980
Efficient cache server memcached (ii)