Memcache介紹:danga.com的一個開源項目,它是一個高性能的分佈式的內存對象緩存系統php
經過在內存裏維護一個統一的巨大的Hash表,可以用來存儲各類格式的數據html
* 查看當前的memcache鏈接數apache
# netstat -n | grep :11211 | wc -lvim
* 查看默認容許的鏈接數,這裏設置的11211端口默認鏈接數是40960緩存
# cat /etc/rc.local服務器
* 如何查看memcache服務器端版本分佈式
# ./memcached -hide
* Linux下的Memcache安裝memcached
# cd /usr/local/src性能
裝yum擴展源:# yum install -y epel-release
memcache模塊:# yum install -y php-pecl-memcached
下載Memcache:# wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
下載libevent:# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz
先裝libevent:# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure --prefix=/usr/local/libevent
# make
# make install
* 測試libevent是否安裝成功
# ls -al /usr/local/libevent/lib | grep libevent
lrwxrwxrwx 1 root root 21 Apr 14 13:01 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 264056 Apr 14 13:01 libevent-1.2.so.1.0.3
-rw-r--r-- 1 root root 430388 Apr 14 13:01 libevent.a
-rwxr-xr-x 1 root root 820 Apr 14 13:01 libevent.la
lrwxrwxrwx 1 root root 21 Apr 14 13:01 libevent.so -> libevent-1.2.so.1.0.3
安裝memcached # cd /usr/local/src
# yum install -y libevent-devel
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure --with-libevent=/usr/local/memcached
# make
# make install
* 測試是否成功安裝memcached
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 113084 Apr 14 13:23 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 117439 Apr 14 13:23 /usr/local/bin/memcached-debug
* 安裝Memcache的PHP擴展
1.在http://pecl.php.net/package/memcache,選擇想要下載的memcache版本
2.安裝PHP的memcache擴展
# tar zxvf memcache-2.2.1.tgz
# cd memcache-2.2.1
# /usr/local/php5.6/bin/phpize
運行這個命令就會生成一個腳本,而後編譯擴展
# ./configure --enable-memcache --with-php-config=/usr/local/php5.6/bin/php-config --with-zlib-dir
# make
# make install
# vim /usr/local/php5.6/etc/php.ini
extension_dir = "/usr/local/php5.6/lib/php/extensions/no-debug-non-zts-20131226/"
extension=memcache.so
* memcached的基本設置
1)啓動Memcache的服務器端
# /usr/local/bin/memcached -d -m 100 -u root -l 127.0.0.1 -p 12000 -c 300 -P /tmp/mem cached.pid
-d選項是啓動一個守護進程,
-m是分配給Memcache使用的內存數量,單位是MB,我這裏是10MB,
-u是運行Memcache的用戶,我這裏是root,
-l是監聽的服務器IP地址,若是有多個地址的話,我這裏指定了服務器的IP地址192.168.0.200,
-p是設置Memcache監聽的端口,我這裏設置了12000,最好是1024以上的端口,
2)若是要結束Memcache進程,執行:
# kill `cat /tmp/memcached.pid`
3)重啓apache,service httpd restart
4)Memcache環境測試
運行下面的php文件,若是有輸出This is a test! 就表示環境搭建成功
# cd /var/www/html
# vim hjt.php
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>