在前面章节中我们已经介绍了如何安装 Memcached 服务,接下来我们为大家介绍 PHP 如何使用 Memcached 服务。
PHP Memcache 扩展包下载地址:http://pecl.php.net/package/memcache,你可以下载最新稳定包(stable)。
wget http://pecl.php.net/get/memcache-2.2.7.tgz |
tar -zxvf memcache-2.2.7.tgz |
cd memcache-2.2.7 |
/usr/local/php/bin/phpize |
./configure --with-php-config=/usr/local/php/bin/php-config |
make && make install |
如果你是 PHP7 版本,则需要下载指定分支:
git clone -b php7 https://github.com/php-memcached-dev/php-memcached.git |
注意:/usr/local/php/ 为php的安装路径,需要根据你安装的实际目录调整。
安装成功后会显示你的memcache.so扩展的位置,比如我的:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ |
最后我们需要把这个扩展添加到php中,打开你的php.ini文件在最后添加以下内容:
[Memcache] |
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" |
extension = memcache.so |
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` |
/usr/local/apache2/bin/apachectl restart |
/usr/local/php/bin/php -m | grep memcache |
安装成功会输出:memcache。
或者通过浏览器访问 phpinfo() 函数来查看,如下图:
memcache support | enabled |
---|---|
Version | 3.0.8 |
Revision | $Revision: 329835 $ |
Directive | Local Value | Master Value |
---|---|---|
memcache.allow_failover | 1 | 1 |
memcache.chunk_size | 32768 | 32768 |
memcache.compress_threshold | 20000 | 20000 |
memcache.default_port | 11211 | 11211 |
memcache.hash_function | crc32 | crc32 |
memcache.hash_strategy | consistent | consistent |
memcache.lock_timeout | 15 | 15 |
memcache.max_failover_attempts | 20 | 20 |
memcache.protocol | ascii | ascii |
memcache.redundancy | 1 | 1 |
memcache.session_redundancy | 2 |
2 |