gpt4 book ai didi

php - 持久连接应该如何与 PHP 的 PECL Memcached 插件一起使用?

转载 作者:搜寻专家 更新时间:2023-10-31 21:33:46 25 4
gpt4 key购买 nike

PHP 的 Memcached(不是 Memcache)PECL 插件接受 $persistent_id 作为其构造函数的一部分。

docs说:

By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.

在具有多个网络服务器和多个共享内存缓存服务器的站点上使用(或不使用)$persistent_id 的最佳方式是什么?特别是,如果我总是分配相同的 $persistent_id,“共享实例”是否会成为瓶颈,而不同的 php 进程等待实例?是否可以创建具有相同 $persistent_id 的其他实例,例如,通过竞争条件?如果发生这种情况,这些额外的连接会怎样?进程之间的那些实例会怎样?

至少,根据我的测试,不止一个“连接”可以有相同的$persistent_id。我做了一个简单的测试,我创建了具有相同 $persistent_id 的 Memcached 实例并向它们添加了不同的服务器,然后加载并回显服务器;有时我会得到第一组,有时是第二组,总是使用相同的 $persistent_id。

此外,很明显,将服务器添加到其中一个共享实例时会出现问题。因为 Memcached 允许您多次添加同一台服务器,所以我看到建议在添加服务器之前应该使用 getServerList() 检查服务器,并且只有在服务器列表未填充时才添加它们。根据这些实例的共享程度,这似乎是一个潜在的竞争条件——是否有可能两个进程可以同时将服务器添加到同一实例,从而破坏服务器列表?

有没有一种方法可以在没有竞争条件的情况下在进程之间安全地共享 Memcached 实例?值得做吗?

最佳答案

当您需要一个服务器池时,为您的 MemCache 服务器设置相同的 persistent_id 是一种正常情况。

文档中的示例:

/* Create a persistent instance */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');

/* now $m2 and $m3 share the same connection */

请注意,即使使用永久 ID,您也必须重新添加服务器,请参阅 http://php.net/manual/en/memcached.construct.php#93536

另请参阅此重要说明:http://php.net/manual/en/memcached.construct.php#106865

您的请求是独立的。 PHP 不共享其请求。因此,如果您有 2 个(或更多)具有相同 persistent_id 的请求,它们都将存在于它们自己的进程中。

关于php - 持久连接应该如何与 PHP 的 PECL Memcached 插件一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com