开源项目

知识点

相关文章

更多

最近更新

更多

使用PHP 连接 Memcached 服务

2019-04-26 17:21|来源: 网路

PHP 连接 Memcached 服务

在前面章节中我们已经介绍了如何安装 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是个服务,就你Apache和nginx一样。你可以把memcached分离出来放到另一台新的服务器上,在新的这一台服务器上需要安装memcached,但不需要安装PHP。因为这台服务器上不运行PHP代码,你的PHP代码是放在WEB服务器上运行的。同要的mysql也一样,可以和WEB服务器分离开来。
  • php客户端不处理持续连接。 你需要使用你的池化想法,或者使用支持持久连接的第三方memcached客户端用于php。 像这个: http://github.com/andreiz/php-memcached/tree/master The php client doesn't handle persistent connections. you either need to use your pooling idea, or use a 3rd party memcached client for php ...
  • 我将用一个例子来解释它 $m1 = new MemCached('test'); $m1->addServer('127.0.0.1', 11211); $m2 = new MemCached('test'); $m2->addServer('127.0.0.1', 11211); var_dump($m2->getServerList()); 输出: array(2) { [0]=> array(2) { ["host"]=> string(9) "127.0.0.1" ...
  • https://pypi.python.org/pypi/python-memcached/ : 该软件是memcached内存缓存守护进程的100%Python接口。 它是客户端软件,允许在一个或多个(可能是远程的)memcached服务器中存储值。 这意味着memcached作为独立的守护进程运行,独立于httpd ,并且您的代码可以像在数据库中一样将数据存储在该守护进程中。 实际上, memcached只不过是NoSQL数据库。 有关教程,请参阅Python中使用的python-memcache(me ...
  • memcached使用libmamcached,根据PHP Windows构建团队,它不能在Windows上编译。 因此,也无法构建扩展。 memcached uses libmamcached, which -- according to the PHP Windows build team -- doesn't compile on windows. Therefore the extension can't be built either.
  • 这是我正在寻找的东西:Memcached :: getStats(); 所以,我写道: add_memcached_server($m, $addr, $port) { $m->addServer($addr,$port); $statuses = $m->getStats(); return isset($statuses[$addr.":".$port]); } 奇迹般有效... This is kinda what I was looking for: Memcached:: ...
  • 我从来没有听说过你的花括号表示法,也找不到任何关于它的信息。 我不会说它不存在。 但是,最简单的操作方法是使用不同的分隔符,这些分隔符不能被误解为变量的一部分。 喜欢这个: set $memcached_key $cookie_devicetype:$request_uri; I have never heard of your curly bracket notation and cannot find any information about it. I won't say it doesn't e ...
  • 从上面重新发布我的评论,以便我可以将此帖子标记为已回答: 找到了我需要改变的选项。 这是OPT_CONNECT_TIMEOUT我将其更改为10但仍需要在我们的专用网络上进行一些测试,看看它是否太低而无法实际使用。 但它解决了我的开发框中的问题。 self::$memcache->setOption(Memcached::OPT_CONNECT_TIMEOUT, 10); Reposting my comment from above so I can mark this post as answered: ...
  • 以下是一些方法可以让它更快。 您的总连接数实际上是为memcached创建了多少个连接。 首先,使用memcached php扩展而不是memcache扩展。 它们是完全不同的,memcache扩展已经非常被弃用了。 Memcached扩展使用libmemcached,它非常快,并且具有更好的功能(如二进制协议,更好的超时,udp) 其次,使用持久连接。 对于您的工作负载,这些应该是完全足够的,并降低不断重新连接到memcache的成本。 第三,使用multi get / set / delete / et ...
  • 您可以使用getStats检查服务器的统计信息: addServer('localhost', 11211); print_r($m->getStats()); ?> You can use getStats to check the stats of your servers: addServer('localhost', 11211); print_r($m->getSt ...