与新的Mongo PHP驱动程序的持久连接 [英] Persistent connections with new Mongo PHP driver

查看:108
本文介绍了与新的Mongo PHP驱动程序的持久连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我正在使用带库的PHP新MongoDB驱动程序(请参阅: https ://docs.mongodb.com/php-library/master/),而不是旧版.

For a project I am using the new MongoDB driver for PHP with the library (see: https://docs.mongodb.com/php-library/master/), not the legacy one.

我们正在将PHP 7作为FPM运行.

We are running PHP 7 as FPM.

最近,我对XDEBUG进行了性能分析,并意识到第一个数据库查询的速度明显慢于第二个数据库查询

I recently did some profiling with XDEBUG and realized that the first database query is significantly slower than the second one, for example

Session::validate -> Account::find 38ms
getProfile        -> Account::find 2ms

Account::find将在其中执行查询以通过其_id获取一个文档,并且两个调用都查找不同的文档.

Where Account::find would execute the query to obtain one document by its _id and both calls look up different documents.

我提出的两个问题

  1. 据我了解,第一个查询比第二个查询慢得多,因为驱动程序必须与数据库服务器建立连接,并且在我们的情况下还必须进行身份验证.脚本执行之间不会保持此连接.正确吗?
  2. 如果(1)为true,是否有任何方法可以保持与新驱动程序的连接?据我了解,与旧驱动程序的连接可以在两次请求之间存储到数据库. ( http://php.net/manual/de/mongo.connecting.persistent. php )

推荐答案

github上的某人将我引向了

Someone on github pointed me towards this site, so I can now answer my own question. To quote:

从1.2.0开始的所有版本的驱动程序都将»libmongoc客户端对象保留在PHP工作进程中,从而使该驱动程序可以跨多个请求重用数据库连接,身份验证状态和拓扑信息.

All versions of the driver since 1.2.0 persist the » libmongoc client object in the PHP worker process, which allows it to re-use database connections, authentication states, and topology information across multiple requests.

但是:

1.2.0之前版本的PHP驱动程序使用PHP的Streams API进行数据库连接,并使用»libmongoc中的API来指定用于套接字通信的自定义处理程序;但是,将为每个MongoDB \ Driver \ Manager创建一个新的libmongoc客户端.结果,驱动程序将保留单个数据库连接,但不保留身份验证状态或拓扑信息.这意味着驱动程序需要在每个请求的开始时发出命令,以进行身份​​验证和»发现服务器拓扑.

Versions of the PHP driver before 1.2.0 utilize PHP's Streams API for database connections, using an API within » libmongoc to designate custom handlers for socket communication; however, a new libmongoc client is created for each MongoDB\Driver\Manager. As a result, the driver persists individual database connections but not authentication state or topology information. This means that the driver needs to issue commands at the start of each request to authenticate and » discover the server topology.

由于撰写本文时最新的稳定驱动程序版本为1.1.9,因此不会保留身份验证信息.但是,您可以升级到1.2.0 alpha版本,这可以大大加快连接速度,正如我在系统上的一些测试中发现的那样. 1.2.0 Alphas目前存在一些已知的错误,因此,我现在将继续使用较慢的1.1.9.

Since the latest stable driver version as of writing this is 1.1.9, the authentication information are not persisted. However you can upgrade to 1.2.0 alpha releases, which can seriously speed up the process of connecting, as I discovered in some tests on my system. There are a few known bugs with the 1.2.0 alphas currently, so I will be sticking with the slower 1.1.9 for now.

此回复将很快过时...

This reponse will soon be outdated...

这篇关于与新的Mongo PHP驱动程序的持久连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆