Basic principle:
① the first time a client accesses an application, it takes the data out of the database (RDBMS), returns it to the client, and saves the extracted data to the memcached.
② the second visit, because the data has been cached, you do not have to go to the database query, directly from the memcached to take.
An RDBMS is a file-type database that is eventually saved as a file on disk, whereas memcached is a key:value relational database that is stored in memory. Memory reads and writes faster than the disk reads and writes, the former being 10 of the latter's 6 times.
Memcached is based on Libevent event handling. Libevent is a library that encapsulates the epoll of Linux, the kqueue of BSD-like operating systems and other event-handling functions into a unified interface. O (1) performance can be played even if the number of connections to the server increases. Memcached uses this libevent library to perform its high performance on Linux, BSD, Solaris, and other operating systems. About event handling here is no longer detailed, you can refer to Dan Kegel's the c10k Problem. For more information about libevent:http://monkey.org/~provos/libevent/.
Compile and install memcached
1, because memcached is based on libevent, so need to install Libevent,libevent-devel
# yum Install libevent libevent-devel-y
2, download and extract memcached-1.4.6.tar.gz
Memcached official website is: http://memcached.org/
# TAR-XVZF Memcached-1.4.6.tar.gz
3. Compile and install memcached-1.4.6
# CD memcached-1.4.6
#./configure--prefix=/etc/memcached
# make
# make Install
4. Configure environment variables (this step can be ignored ...)
Enter the user hosting directory, edit. Bash_profile, add a new directory for System environment variable Ld_library_path, and the additional content will be as follows:
# Vi. Bash_profile
Memcached_home=/etc/memcached
Export ld_library_path= $LD _library_path: $MEMCACHED _home/lib
Refresh User environment variable: # source. bash_profile
5. Write memcached service start and stop script
# CD/ETC/INIT.D
VI memcached, the script content is as follows:
#!/bin/sh # Startup script for the server of memcached # processname:memcached # pidfile:/ETC/MEMCACHED/MEMCAC Hed.pid # logfile:/etc/memcached/memcached_log.txt # memcached_home:/etc/memcached # chkconfig:35 On:start and Stop memcached Service # Source function library. /etc/rc.d/init.d/functions retval=0 prog= "memcached" basedir=/etc/memcached cmd=${basedir}/bin/memcached "$ Basedir/${prog}.pid "#logfile =" $basedir/memcached_log.txt "ipaddr=" 192.168.1.200 "# Binding listening IP address port=" 11211 "# Dress Service port Username= "root" # The user identity of the running program max_memory=64 # default:64m | Max use memory max_simul_conn=1024 # default:1024 | Maximum number of simultaneous connections #maxcon =51200 #growth_factor =1.3 # default:1.25 | Block size growth factor #thread_num =6 # Default:4 #verbose = "-VV" # View detailed startup information #bind_protocol =binary # ASCII, binary, or aut O (default) Start () {Echo-n $ "starting service: $prog" $cmd-D-M $max _memory-u $username-L $IPADDR-P $port-C $ Max_simul_conn-P $pidfile retval=$? echo [$RETVAL-eq 0] && touch/var/lock/subsys/$prog} stop () {echo-n $ "stopping service: $prog" Run_ User= ' WhoAmI ' pidlist= ' ps-ef | grep $run _user | grep memcached | Grep-v grep | awk ' {print ($)} ' for PID $pidlist do # echo "pid= $pid" kill-9 $pid if [$-ne 0];
Then return 1 fi do retval=$?
echo [$RETVAL-eq 0] && rm-f/var/lock/subsys/$prog} # to how we were called.
Case "in Start" start;;
stop) stop;;
#reload) # reload #;;
restart) stop start;; #condrestart) # If [-f/var/lock/subsys/$prog];
Then # stop # start # fi #
status) status memcached;; *) echo "Usage: $ {Start|stop|restart|status}" Exit 1 Esac Exit $RETVAL
6. Give execution permission
#chmod +x memcached
7, set up memcached with the system to start
# chkconfig--add memcached
# chkconfig--level memcached on
Start memcached
# service memcached Start
When it starts, it actually calls the following command to start the memcached in a daemon way.
/etc/memcached/bin/memcached-d-M 64-u root-l 192.168.1.201 \
-P 11211-c 1024-p/etc/memcached/memcached.pid
To see if memcached is started
# Ps-ef | grep memcached
Installing Memcache PHP Extensions
1. Select the Memcache version that you want to download in Http://pecl.php.net/package/memcache.
2. Install the PHP memcache extension
Tar vxzf memcache-2.2.5.tgz
CD memcache-2.2.5
/usr/local/php/bin/phpize
./configure--enable-memcache--with-php-config=/usr/local/php/bin/php-config--with-zlib-dir
Make
Make install
3. The above installation will have a similar hint:
Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
4. Change the Extension_dir = "./" in php.ini to
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
5. Add a row to load the memcache extension: extension=memcache.so
Next reboot PHP is OK, you can see through the Phpinfo test page