I. concept
1. What is memcache
-
Memcached is a free, open source, high performance, distributed memory object caching system
-
Memcached is a software developed by Brad Fitzpatric of Danga Interactive, a company owned by LiveJournal. Now it has become an important factor to improve the extensibility of Web applications in many services such as mixi, hatena, Facebook, Vox, LiveJournal and so on.
-
Memcached is a memory-based key-value store for storing small pieces of arbitrary data (strings, objects). These data can be the result of database calls, API calls, or page rendering.
-
Memcached is simple and powerful. Its concise design facilitates rapid development, reduces the difficulty of development, and solves many problems of large data cache. Its API is compatible with most popular development languages.
-
Essentially, it is a concise key-value storage system.
-
The general purpose is to reduce the number of database accesses by caching the results of database queries, so as to improve the speed and scalability of dynamic Web applications. (For frequent reading, with high repetition rate and low update frequency, memcache can optimize the response speed of your system.)
2. memcache usage scenarios
-
Frequent access to dictionary data
-
Lots of hot data
-
Page cache
-
Frequent query conditions and results
-
Temporary Processing Data
3. The Difference between memcached and memcached
-
Memcache is the name of the project
-
memcached is the name of the main program file on its server side.
2. The combination of memcache and php to add cache to php
1) configuration
Prepare the memcache installation package
1> decompression
[root@server1 mysql]# cd [root@server1 ~]# cd lnmp/ [root@server1 lnmp]# tar xf memcache-2.2.5.tgz [root@server1 lnmp]# cd memcache-2.2.5
2 > Modify environment variables to add compilation commands
-
Phpize is used to extend PHP extension module. phpize can build PHP plug-in module. For example, if you want to add memcached or ImageMagick to the original compiled php, you can use phpize.
-
Phpize tool is in the PHP installation directory, based on this point, phpize corresponds to the PHP environment at that time, so it is necessary to generate the corresponding configure file according to the configuration of the php, and establish a configure file. You have to run phpize in a directory, so phpize knows which directory your environment is, and the configuration file is built in that directory.
[root@server1 memcache-2.2.5]# vim ~/.bash_profile 10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin ##Add. / configure command [root@server1 memcache-2.2.5]# source ~/.bash_profile [root@server1 memcache-2.2.5]# phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226
3 > Compile and Install
[root@server1 memcache-2.2.5]# ./configure [root@server1 memcache-2.2.5]# make && make install
4 > View the generated directory
Installation completes to generate / usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226 / directory
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/ [root@server1 no-debug-non-zts-20131226]# ls ##View directory content memcache.so opcache.a opcache.so
5 > Modify the php configuration file to connect php to memcache
[root@server1 no-debug-non-zts-20131226]# vim /usr/local/lnmp/php/etc/php.ini 873 extension=memcache.so [root@server1 no-debug-non-zts-20131226]# /etc/init.d/php-fpm reload Reload service php-fpm done
6 > Install memcached (for native recognition)
It's a daemon process (11211 for native access)
[root@server1 no-debug-non-zts-20131226]# yum install memcached -y [root@server1 no-debug-non-zts-20131226]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="" [root@server1 no-debug-non-zts-20131226]# /etc/init.d/memcached start Starting memcached: [ OK ]
7 > Install telnet tools
[root@server1 no-debug-non-zts-20131226]# yum install telnet -y [root@server1 no-debug-non-zts-20131226]# netstat -antlpe|grep memcached tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 498 22061 8292/memcached tcp 0 0 :::11211 :::* LISTEN 498 22062 8292/memcached [root@server1 no-debug-non-zts-20131226]# telnet localhost 11211 Trying ::1... Connected to localhost. Escape character is '^]'. stats
quit #Sign out [root@server1 no-debug-non-zts-20131226]# telnet localhost 11211 Trying ::1... Connected to localhost. Escape character is '^]'. set name 0 0 6 ##Set key-value to store KEY values, serial cache time, VALUE value length westos STORED get name VALUE name 0 6 westos END set name 0 5 6 ##Add 0 is number saved 5 miracles 6 is value length westos STORED get name ##See END quit Connection closed by foreign host.
8 > Copy memcache publishing page to nginx publishing directory and configure it
[root@server1 no-debug-non-zts-20131226]# cd /root/lnmp/memcache-2.2.5 [root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/nginx/html/ [root@server1 memcache-2.2.5]# cd /usr/local/nginx/html/ [root@server1 html]# ls 50x.html bbs example.php index.html index.php memcache.php readme utility [root@server1 html]# vim memcache.php 23 define('ADMIN_PASSWORD','westos'); ##Password 28 $MEMCACHE_SERVERS[] = '172.25.47.1:11211'; // add more as an array 29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array ## Note, comments in this line [root@server1 html]# /etc/init.d/php-fpm reload ##Refresh php
2) test
Browser Http://172.25.47.1/memcache.php#web monitoring page
Physical reality machine
[kiosk@foundation47 ~]$ ab -c 10 -n 5000 http://172.25.47.1/index.php # ordinary PHP
[kiosk@foundation47 ~]$ ab -c 10 -n 5000 http://172.25.47.1/example.php ## php with cache
Significant speed increase
Look at the monitoring page