php的memcache模塊:
下載地址:http://pecl.php.net/package/memcache
下載并配置生成php的memcache模塊
wget http://pecl.php.net/get/memcache-3.0.8.tgz
yum install -y m4 autoconf
tar -zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
凌源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
/usr/local/php/bin/phpize //生成.config文件
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /usr/local/php/ext 創(chuàng)建一個(gè)統(tǒng)一的存放擴(kuò)展目錄
打開php.ini
如果沒有php.ini {cp /usr/local/src/php-5.3.27/php.ini-production /usr/local/php/etc/php.ini}
添加:extension_dir = "/usr/local/php/ext"定義一個(gè)統(tǒng)一的存放目錄。
cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so /usr/local/php/ext 復(fù)制模塊進(jìn)目錄里
然后再編輯php.ini在最后添加一行extension = memcache.so
檢測(cè)一下/usr/local/php/bin/php -m |grep memcache 查看模塊是否已經(jīng)加載上。
用一個(gè)php腳本:
//連接Memcache Memcache ip地址可以改
$mem = new Memcache;
$mem->connect("192.168.47.26", 11211);
//保存數(shù)據(jù)
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."
";
//替換數(shù)據(jù)
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//保存數(shù)組數(shù)據(jù)
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//刪除數(shù)據(jù)
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//清除所有數(shù)據(jù)
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//關(guān)閉連接
$mem->close();
?>
實(shí)現(xiàn)session共享:
在php.ini里配置連接:
添加:
session.save_handler = "memcache"
session.save_path = "tcp://192.168.47.26:11211"
在httpd.conf【LAMP】里配置連接:apache
虛擬主機(jī)配置添加:
php_value session.save_handler "memcache"
php_value session.save_path "tcp://192.168.47.26:11211"
在php-fpm.conf【LNMP】里配置連接:nginx
對(duì)應(yīng)的pool里面添加:
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.47.26:11211"