memcache是一套分佈式的高速緩存系統 目前被許多網站使用以提高網站的訪問速度,尤爲對於一些大型的、須要頻繁訪問數據庫的網站訪問速度提高效果十分顯著[1]
1.先檢查客戶端訪問的數據是否在於memcache,若是有就直接返回 2.若是不在memcache,就去查數據庫,同時緩存一份到memcache,大大提升讀取速度。
1.用來作網頁或數據庫高速緩存 2.可用來作session共享 3.適用於數據變更小但多(如微博粉絲+1) 4.存儲在內存,不能數據持久化
20%:熱數據,常常被訪問的數據。用做緩存,存於內存 80%:基本不變化的數據,存於固態硬盤
檢測當前php環境php
vim ~/.bash_profilehtml
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin/:/usr/local/lnmp/php/bin
. ~/.bash_profile 或者作軟連接mysql
[root@server11 bin]# ln -s /usr/local/lnmp/php/bin /usr/local/bin/nginx
tar zxf memcache-2.2.5.tgz cd memcache-2.2.5sql
phpize 準備預編譯環境數據庫
./configure make && make installvim
保證php的執行路徑是源碼包的路徑瀏覽器
[root@server11 memcache-2.2.5]# which php /usr/local/lnmp/php/bin/php緩存
cd /usr/local/lnmp/php/etc/bash
vim php.ini 記住是.
863 extension=memcache.so
/etc/init.d/php-fpm start
[root@server11 etc]# php -m |grep memcache
rpm -qa |grep php 保證沒有rpm包乾擾
空
yum install memcached -y
/etc/init.d/memcached start
監聽端口 netstat -antlpue
udp 0 0 0.0.0.0:11211 0.0.0.0:* 498 10940 3706/memcached
yum install telnet -y
telnet localhost 11211
set name 0 0 6 westos STORED get name VALUE name 0 6 westos END delete name DELETED get name END
cd memcache-2.2.5
cp memcache.php /usr/local/nginx/html/
vim memcache.php
23 define('ADMIN_PASSWORD','westos'); // Admin Password 28 $MEMCACHE_SERVERS[] = ''; // add more as an array 29 $MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an arra y
vim test.php
<?php $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version."\n"; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server"); echo "Store data in the cache (data will expire in 10 seconds)\n"; $get_result = $memcache->get('key'); echo "Data from the cache:\n"; var_dump($get_result); ?>
nginx
檢驗
在瀏覽器訪問:
1. 172.25.88.11/memcache.php 監控cache命中率
2. 172.25.88.11/test.php
不斷刷新,能夠在監控頁面看到,緩存的命中率(Hits)愈來愈大