php有两个memcache客户端:php memcache和php memcached。
php memcache独立用php实现,是老客户端,功能少,属性也可设置的少;
http://pecl.php.net/package/memcache
php memcached是基于原生的c的libmemcached的扩展,更加完善,建议替换为php memcached。
http://pecl.php.net/package/memcached
安装php memcache扩展参考: centos yum安装memcached及php memcache扩展
yum -y install gcc+ gcc-c++ php-devel zlib-devel
2、通过yum安装memcached服务端
yum -y install memcached
#安装完成后执行:
memcached -h
#出现memcached帮助信息说明安装成功
memcached 1.4.4
4、 配置memcached
vim /etc/sysconfig/memcached
#文件中内容如下,按需要修改:
PORT="11211" #端口
USER="root" #用户名
MAXCONN="1024" #最大连接数
CACHESIZE="64" #内存大小
OPTIONS="" #附加参数
5、安装libmemached
wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz
tar -zxvf libmemcached-1.0.16.tar.gz
cd libmemcached-1.0.16
./configure -prefix=/usr/local/libmemcached -with-memcached
make && make install
6、安装PHP Memcached扩展
wget http://pecl.php.net/get/memcached-2.1.0.tgz
tar -zxvf memcached-2.1.0.tgz
cd memcached-2.1.0
/usr/bin/phpize
./configure -enable-memcached -with-php-config=/usr/bin/php-config -with-zlib-dir -with-libmemcached-dir=/usr/local/libmemcached -prefix=/usr/local/phpmemcached
make && make install
记录下安装成功后的提示,类似于:
Installing shared extensions: /usr/lib64/php/modules/
7、增加扩展extension配置文件
vi /etc/php.d/memcached.ini
增加1行
extension=memcached.so
最后验证一下是否安装完成
php -m|grep memcached
会显示:
memcached
8、重启nginx/amapche,重启php-fpm,使用phpinfo()查看memcached模块是否安装成功。
9.1、启动memcached
service memcached start
提示:
Starting memcached: [ OK ]
9.2、终端telnet测试
如果提示bash: telnet: command not found
执行:yum install telnet -y
[root@localhost memcached-2.1.0]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
9.3、php测试:
<?php
$m = new Memcached;
$m->addServer('127.0.0.1', 11211);
$m->set('foo', 200);
var_dump($m->get('foo')); //int(200)
php memcache独立用php实现,是老客户端,功能少,属性也可设置的少;
http://pecl.php.net/package/memcache
php memcached是基于原生的c的libmemcached的扩展,更加完善,建议替换为php memcached。
http://pecl.php.net/package/memcached
安装php memcache扩展参考: centos yum安装memcached及php memcache扩展
安装memached及php memached扩展步骤如下:
yum -y install gcc+ gcc-c++ php-devel zlib-devel
2、通过yum安装memcached服务端
yum -y install memcached
#安装完成后执行:
memcached -h
#出现memcached帮助信息说明安装成功
memcached 1.4.4
......
3. 加入启动服务
chkconfig --level 2345 memcached on4、 配置memcached
vim /etc/sysconfig/memcached
#文件中内容如下,按需要修改:
PORT="11211" #端口
USER="root" #用户名
MAXCONN="1024" #最大连接数
CACHESIZE="64" #内存大小
OPTIONS="" #附加参数
5、安装libmemached
wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz
tar -zxvf libmemcached-1.0.16.tar.gz
cd libmemcached-1.0.16
./configure -prefix=/usr/local/libmemcached -with-memcached
make && make install
6、安装PHP Memcached扩展
wget http://pecl.php.net/get/memcached-2.1.0.tgz
tar -zxvf memcached-2.1.0.tgz
cd memcached-2.1.0
/usr/bin/phpize
./configure -enable-memcached -with-php-config=/usr/bin/php-config -with-zlib-dir -with-libmemcached-dir=/usr/local/libmemcached -prefix=/usr/local/phpmemcached
make && make install
记录下安装成功后的提示,类似于:
Installing shared extensions: /usr/lib64/php/modules/
7、增加扩展extension配置文件
vi /etc/php.d/memcached.ini
增加1行
extension=memcached.so
最后验证一下是否安装完成
php -m|grep memcached
会显示:
memcached
8、重启nginx/amapche,重启php-fpm,使用phpinfo()查看memcached模块是否安装成功。
9.1、启动memcached
service memcached start
提示:
Starting memcached: [ OK ]
9.2、终端telnet测试
如果提示bash: telnet: command not found
执行:yum install telnet -y
[root@localhost memcached-2.1.0]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
9.3、php测试:
<?php
$m = new Memcached;
$m->addServer('127.0.0.1', 11211);
$m->set('foo', 200);
var_dump($m->get('foo')); //int(200)
?>