PHP 连接 Memcached 服务

Memcached 教程 · 2019-03-09 07:57:27

在前面章节中我们已经介绍了如何安装 Memcached 服务,接下来我们为大家介绍 PHP 如何使用 Memcached 服务。

PHP Memcache 扩展安装

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

注意:/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

添加完后 重新启动php,我使用的是nginx+php-fpm进程所以命令如下:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

如果是apache的使用以下命令:

/usr/local/apache2/bin/apachectl restart

检查安装结果

/usr/local/php/bin/php -m | grep memcache

安装成功会输出:memcache。

或者通过浏览器访问 phpinfo() 函数来查看,如下图:

memcache-php

PHP 连接 Memcached

<?php
$memcache = new Memcache;             //创建一个memcache对象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器
$memcache->set('key', 'test');        //设置一个变量到内存中,名称是key 值是test
$get_value = $memcache->get('key');   //从内存中取出key的值
echo $get_value;
?>

更多 PHP 操作 Memcached 请参阅: http://php.net/manual/zh/book.memcache.php

点击查看所有 Memcached 教程 文章: https://codercto.com/courses/l/34.html

查看所有标签

Convergence Culture

Convergence Culture

Henry Jenkins / NYU Press / 2006-08-01 / USD 30.00

"Convergence Culture" maps a new territory: where old and new media intersect, where grassroots and corporate media collide, where the power of the media producer, and the power of the consumer intera......一起来看看 《Convergence Culture》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具