基本原理:
① 客戶端第一次訪問應用程序時,會到數據庫(rdbms)中取出數據,返回給客戶端;同時也將取出的數據保存到memcached中。
② 第二次訪問時,因為數據已經緩存,就不用去數據庫查詢了,直接從memcached中取。
rdbms是文件型的數據庫,最終還是以文件的形式保存在磁盤上;而memcached則不一樣,它是key:value關系型的數據庫,是保存在內存中的。內存的讀寫速度要比磁盤的讀寫速度快得多,前者是后者的10的6次方倍。
memcached是基于libevent的事件處理。libevent是個程序庫,它將linux的epoll、bsd類操作系統的kqueue等事件處理功能封裝成統一的接口。即使對服務器的連接數增加,也能發揮o(1)的性能。 memcached使用這個libevent庫,因此能在linux、bsd、solaris等操作系統上發揮其高性能。關于事件處理這里就不再詳細介紹,可以參考dan kegel的the c10k problem。
編譯安裝memcached
1、由于memcached是基于libevent的,因此需要安裝libevent,libevent-devel
# yum install libevent libevent-devel -y
2、下載并解壓memcached-1.4.6.tar.gz
memcached官方網站是:
# tar -xvzf memcached-1.4.6.tar.gz
3、編譯安裝memcached-1.4.6
# cd memcached-1.4.6
# ./configure –prefix=/etc/memcached
# make
# make install
4、配置環境變量(這一步可忽略…)
進入用戶宿主目錄,編輯.bash_profile,為系統環境變量ld_library_path增加新的目錄,需要增加的內容如下:
# vi .bash_profile
memcached_home=/etc/memcached
export ld_library_path=$ld_library_path:$memcached_home/lib
刷新用戶環境變量:# source .bash_profile
5、編寫memcached服務啟停腳本
# cd /etc/init.d
vi memcached,腳本內容如下:
#!/bin/sh # #?startup?script?for?the?server?of?memcached # #?processname:?memcached #?pidfile:?/etc/memcached/memcached.pid #?logfile:?/etc/memcached/memcached_log.txt #?memcached_home:?/etc/memcached #?chkconfig:?35?21?79 #?description:?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 pidfile="$basedir/${prog}.pid" #logfile="$basedir/memcached_log.txt" ipaddr="192.168.1.200" ??#?綁定偵聽的ip地址 port="11211" ??#?服務端口 username="root" ?#?運行程序的用戶身份 max_memory=64 ??#?default:?64m?|?最大使用內存 max_simul_conn=1024 ?#?default:?1024?|?最大同時連接數 #maxcon=51200 #growth_factor=1.3 ?#?default:?1.25?|?塊大小增長因子 #thread_num=6 ??#?default:?4 #verbose="-vv" ??#?查看詳細啟動信息 #bind_protocol=binary ??#?ascii,?binary,?or?auto?(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($2)}'` for?pid?in?$pidlist do # ??echo?"pid=$pid" kill?-9?$pid if?[?$??-ne?0?];?then return?1 fi done retval=$? echo [?$retval?-eq?0?]?&&?rm?-f?/var/lock/subsys/$prog } #?see?how?we?were?called. case?"$1"?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:?$0?{start|stop|restart|status}" exit?1 esac exit?$retval
6、賦予執行權限
#chmod +x memcached
7、設置memcached隨系統啟動
# chkconfig –add memcached
# chkconfig –level 35 memcached on
啟動memcached
# service memcached start
//啟動的時候實際上是調用了下面的這個命令,以守護進程的方式來啟動memcached
/etc/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.201
-p 11211 -c 1024 -p /etc/memcached/memcached.pid
查看memcached是否啟動
# ps -ef | grep memcached
安裝memcache的php擴展
1.在 選擇相應想要下載的memcache版本。
2.安裝php的memcache擴展
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.上述安裝完后會有類似這樣的提示:
installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
4.把php.ini中的extension_dir = “./”修改為
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”
5.添加一行來載入memcache擴展:extension=memcache.so
接下來重啟php就可以了,可以通過phpinfo測試頁面查看