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
/usr/local/php/bin/phpize //生成.config文件
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir /usr/local/php/ext 创建一个统一的存放扩展目录
打开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"定义一个统一的存放目录。
cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/memcache.so /usr/local/php/ext 复制模块进目录里
然后再编辑php.ini在最后添加一行extension = memcache.so
检测一下/usr/local/php/bin/php -m |grep memcache 查看模块是否已经加载上。
用一个php脚本:
<?php
//连接Memcache Memcache ip地址可以改
$mem = new Memcache;
$mem->connect("192.168.47.26", 11211);
//保存数据
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."<br>";
//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//保存数组数据
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "<br>";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br>";
//关闭连接
$mem->close();
?>
实现session共享:
在php.ini里配置连接:
添加:
session.save_handler = "memcache"
session.save_path = "tcp://192.168.47.26:11211"
在httpd.conf【LAMP】里配置连接:apache
虚拟主机配置添加:
php_value session.save_handler "memcache"
php_value session.save_path "tcp://192.168.47.26:11211"
在php-fpm.conf【LNMP】里配置连接:nginx
对应的pool里面添加:
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://192.168.47.26:11211"