03-PHP-memcached

时间:2023-02-01 13:00:45
【安装memcached】
[安装 libevent]
$ tar zxvf libevent-2.0.20-stable.tar.gz
$ cd libevent-2.0.20-stable/
$ ./configure --prefix=/usr/local/libevent
$ make && make install
注:Mac下可能会出错:bufferevent_openssl.c:60:10: fatal error: 'openssl/bio.h' file not found,解决方案:

brew install openssl

brew link openssl --force

cp -R /usr/local/Cellar/openssl/1.0.2h_1/include/openssl ~/software/libevent-2.0.22-stable

 
[安装memcached服务端]
$ tar zxvf memcached-1.4.39.tar.gz
$ ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
$ make && make install
 
[启动]
$ cd /usr/local/memcached
$ ./memcached -u root –d    #启动memcached
$ ps -ef |grep memcached #查看memcached运行状态
 
【备注】设置memcached开机启动,vim打开/etc/rc.local在最后面写入:
/usr/local/memcached/bin/memcached -u root -d
 
[连接]
$telnet 127.0.0.1 11211
stats --查看状态
version --查看版本
set user 1 3000 10 --添加数据
get user --获取数据
delete user --删除数据
flush_all --清空所有
 
安装php-memcached扩展】
[安装 libmemcached]
$ tar zxvf libmemcached-1.0.18.tar.gz
$ cd libmemcached-1.0.18/
$ ./configure --prefix=/usr/local/libmemcached --with-memcached --enable-sasl
$ make && make install
注:Mac下可能会出错:error: use of undeclared identifier 'ntohll',解决方案:
()编辑libmemcached/byteorder.cc文件
在 #include "libmemcached/byteorder.h" 下面增加以下内容:
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

()、编辑clients/memflush.cc文件
将两处 if (opt_servers == false)
替换成 if (opt_servers == NULL)
 
 
[安装 php-memcached扩展]
$ unzip php-memcached-php7.zip
$ cd php-memcached-php7/
$ /usr/local/php/bin/phpize
$ ./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --enable-memcached-sasl
$ make && make install
  安装完成后,在php.ini(/usr/local/php/php.ini) 后面添加 extension=memcached.so
 
 
 [PHP操作memcached]
<?php
$mem = new Memcached; //创建一个memcache对象
$mem->addServer('127.0.0.1', 11211) or die ("Could not connect"); //连接Memcached服务器
$mem->set('key', 'test'); //设置一个变量到内存中,名称是key 值是test
$val = $mem->get('key'); //从内存中取出key的值
echo $val;
?>
 
 
 
 
 
 
 

相关文章