首先安装好memcache软件,安装请查询相关资料
安装libmemcached
下载:https://launchpad.net/libmemcached/+download
# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
# tar zxvf libmemcached-1.0.18.tar.gz
# cd libmemcached-1.0.18
# yum -y install gcc44 gcc44-c++ libstdc++44-devel
# export CC=/usr/bin/gcc44
# export CXX=/usr/bin/g++44
#./configure --prefix=/usr/local/libmemcached --enable-sasl #SASL全称Simple Authentication and Security Layer,是一种用来扩充C/S模式验证能力的机制
# make && make install
安装php扩展memcached
下载:http://pecl.php.net/package/memcached
# wget http://pecl.php.net/get/memcached-2.2.0.tgz
# tar zxvf http://pecl.php.net/get/memcached-2.2.0.tgz
# cd memcached-2.2.0
# yum install zlib-devel ###安装zlib压缩
没有configure文件,用phpize生成configure文件
# /usr/local/php/bin/phpize 产生configure文件
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/ --enable-memcached-sasl ##libmemcached安装路径,红色部分重点
# make && make install
# 编辑php.ini,加入extension=memcached.so memcached.use_sasl = 1
启动 memcached进程
# memcached -p 11211 -m 1024 -u root -d
使用libmemcached
# memstat --servers=127.0.0.1:11211
PHP 使用查询php手册
<?php
$m=new memcached();
$m->addServer('127.0.0.1',11211,10);
$m->setOption(Memcached::OPT_COMPRESSION,0.2); //启用压缩