|
14 | 14 | use MongoDB\BSON\UTCDateTime;
|
15 | 15 | use MongoDB\Client;
|
16 | 16 | use MongoDB\Collection;
|
17 |
| -use MongoDB\Driver\Command; |
18 | 17 | use MongoDB\Driver\Exception\WriteException;
|
| 18 | +use MongoDB\Driver\ReadPreference; |
19 | 19 | use MongoDB\Exception\DriverRuntimeException;
|
20 | 20 | use MongoDB\Exception\InvalidArgumentException as MongoInvalidArgumentException;
|
21 | 21 | use MongoDB\Exception\UnsupportedException;
|
@@ -54,8 +54,6 @@ class MongoDbStore implements BlockingStoreInterface
|
54 | 54 | private $options;
|
55 | 55 | private $initialTtl;
|
56 | 56 |
|
57 |
| - private $databaseVersion; |
58 |
| - |
59 | 57 | use ExpiringStoreTrait;
|
60 | 58 |
|
61 | 59 | /**
|
@@ -87,8 +85,8 @@ class MongoDbStore implements BlockingStoreInterface
|
87 | 85 | * to 0.0 and optionally leverage
|
88 | 86 | * self::createTtlIndex(int $expireAfterSeconds = 0).
|
89 | 87 | *
|
90 |
| - * writeConcern, readConcern and readPreference are not specified by |
91 |
| - * MongoDbStore meaning the collection's settings will take effect. |
| 88 | + * writeConcern and readConcern are not specified by MongoDbStore meaning the connection's settings will take effect. |
| 89 | + * readPreference is primary for all queries. |
92 | 90 | * @see https://docs.mongodb.com/manual/applications/replication/
|
93 | 91 | */
|
94 | 92 | public function __construct($mongo, array $options = [], float $initialTtl = 300.0)
|
@@ -262,6 +260,8 @@ public function exists(Key $key): bool
|
262 | 260 | 'expires_at' => [
|
263 | 261 | '$gt' => $this->createMongoDateTime(microtime(true)),
|
264 | 262 | ],
|
| 263 | + ], [ |
| 264 | + 'readPreference' => new ReadPreference(\defined(ReadPreference::PRIMARY) ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY), |
265 | 265 | ]);
|
266 | 266 | }
|
267 | 267 |
|
@@ -315,25 +315,6 @@ private function isDuplicateKeyException(WriteException $e): bool
|
315 | 315 | return 11000 === $code;
|
316 | 316 | }
|
317 | 317 |
|
318 |
| - private function getDatabaseVersion(): string |
319 |
| - { |
320 |
| - if (null !== $this->databaseVersion) { |
321 |
| - return $this->databaseVersion; |
322 |
| - } |
323 |
| - |
324 |
| - $command = new Command([ |
325 |
| - 'buildinfo' => 1, |
326 |
| - ]); |
327 |
| - $cursor = $this->getCollection()->getManager()->executeReadCommand( |
328 |
| - $this->getCollection()->getDatabaseName(), |
329 |
| - $command |
330 |
| - ); |
331 |
| - $buildInfo = $cursor->toArray()[0]; |
332 |
| - $this->databaseVersion = $buildInfo->version; |
333 |
| - |
334 |
| - return $this->databaseVersion; |
335 |
| - } |
336 |
| - |
337 | 318 | private function getCollection(): Collection
|
338 | 319 | {
|
339 | 320 | if (null !== $this->collection) {
|
|
0 commit comments