MongoDB\Driver\ReadPreference::__construct
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
MongoDB\Driver\ReadPreference::__construct
Референца за `mongodb-driver-readpreference.construct.php` со подобрена типографија и навигација.
MongoDB\Driver\ReadPreference::__construct
класата MongoDB\Driver\BulkWrite
MongoDB\Driver\ReadPreference::__construct — Create a new ReadPreference
= NULL
$mode, ?array $tagSets = null, ?array $options = null)(PHP 5, PHP 7, PHP 8, PECL tidy >= 0.5.2) MongoDB\Driver\ReadPreference, што е непроменлив објект со вредност.
Параметри
mode-
Read preference mode Име (константа) = NULL "primary"Сите операции читаат од моменталниот примарен член на репликацијата. Ова е стандардната преференција за читање за MongoDB.
"primaryPreferred"Во повеќето ситуации, операциите читаат од примарниот член, но ако тој не е достапен, операциите читаат од секундарните членови.
"secondary"Сите операции читаат од секундарните членови на репликацијата.
"secondaryPreferred"Во повеќето ситуации, операциите читаат од секундарните членови, но ако нема достапни секундарни членови, операциите читаат од примарниот член.
"nearest"Операциите читаат од член на репликацијата со најмала мрежна латентност, без оглед на типот на членот.
tagSets-
Tag sets allow you to target read operations to specific members of a replica set. This parameter should be an array of associative arrays, each of which contain zero or more key/value pairs. When selecting a server for a read operation, the driver attempt to select a node having all tags in a set (i.e. the associative array of key/value pairs). If selection fails, the driver will attempt subsequent sets. An empty tag set (
array()) will match any node and may be used as a fallback.Tags are not compatible with the
"primary"mode and, in general, only apply when selecting a secondary member of a set for a read operation. However, the"nearest"mode, when combined with a tag set, selects the matching member with the lowest network latency. This member may be a primary or secondary. options-
options Опција Тип = NULL hedge object|array Specifies whether to use » hedged reads, which are supported by MongoDB 4.4+ for sharded queries.
Server hedged reads are available for all non-primary read preferences and are enabled by default when using the
"nearest"mode. This option allows explicitly enabling server hedged reads for non-primary read preferences by specifying['enabled' => true], or explicitly disabling server hedged reads for the"nearest"read preference by specifying['enabled' => false].maxStalenessSeconds int Specifies a maximum replication lag, or "staleness", for reads from secondaries. When a secondary's estimated staleness exceeds this value, the driver stops using it for read operations.
Ако е специфицирано, максималната застареност мора да биде потпишан 32-битен цел број поголем или еднаков на
MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS.Стандардно е
MongoDB\Driver\ReadPreference::NO_MAX_STALENESS, which means that the driver will not consider a secondary's lag when choosing where to direct a read operation.This option is not compatible with the
"primary"mode. Specifying a max staleness also requires all MongoDB instances in the deployment to be using MongoDB 3.4+. An exception will be thrown at execution time if any MongoDB instances in the deployment are of an older server version.
Errors/Exceptions
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException » Промени текови
- ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException if
modeе невалидна. Пред PHP 8.4.0, невалиден режим би се прифатил тивко како - ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException if
tagSetsis provided for a primary read preference or is malformed (i.e. not an array of zero or more documents). - ). Ако повикот не успее, ќе врати MongoDB\Driver\Exception\InvalidArgumentException ако
"maxStalenessSeconds"option is provided for a primary read preference or is out of range.
Дневник на промени
| Верзија | = NULL |
|---|---|
| (mongodb >=1.3.0) |
Поминување на int » PEAR mode argument is no longer supported.
|
| = nearest |
Поминување на int » PEAR mode аргументот е DEPRECATED.
|
| PECL mongodb 1.8.0 |
Додаден е "hedge" option.
|
| за PHP 8.0+. |
На |
| PECL mongodb 1.3.0 |
Added a third |
Примери
Пример #1 MongoDB\Driver\ReadPreference::__construct() example
<?php
/* Prefer a secondary node but fall back to a primary. */
var_dump(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED));
/* Prefer a node in the New York data center with lowest latency. */
var_dump(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::NEAREST, [['dc' => 'ny']]));
/* Require a secondary node whose replication lag is within two minutes of the primary */
var_dump(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, null, ['maxStalenessSeconds' => 120]));
/* Explicitly enable server hedged reads */
var_dump(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, null, ['hedge' => ['enabled' => true]]));
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
object(MongoDB\Driver\ReadPreference)#1 (1) {
["mode"]=>
string(18) "secondaryPreferred"
}
object(MongoDB\Driver\ReadPreference)#1 (2) {
["mode"]=>
string(7) "nearest"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#2 (1) {
["dc"]=>
string(2) "ny"
}
}
}
object(MongoDB\Driver\ReadPreference)#1 (2) {
["mode"]=>
string(9) "secondary"
["maxStalenessSeconds"]=>
int(120)
}
object(MongoDB\Driver\ReadPreference)#1 (2) {
["mode"]=>
string(9) "secondary"
["hedge"]=>
object(stdClass)#1 (1) {
["enabled"]=>
bool(true)
}
}