189 8069 5689

php的memcache模块

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适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信: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 创建一个统一的存放扩展目录 

打开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脚本:

//连接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 ."
";
//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//保存数组数据
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//删除数据
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";
//清除所有数据
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//关闭连接
$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"


文章标题:php的memcache模块
URL标题:http://jkwzsj.com/article/gggjsh.html

其他资讯