LegacyMongoDBCache.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use MongoBinData;
  4. use MongoCollection;
  5. use MongoCursorException;
  6. use MongoDate;
  7. use const E_USER_DEPRECATED;
  8. use function serialize;
  9. use function time;
  10. use function trigger_error;
  11. use function unserialize;
  12. /**
  13. * MongoDB cache provider.
  14. *
  15. * @internal Do not use - will be removed in 2.0. Use MongoDBCache instead
  16. */
  17. class LegacyMongoDBCache extends CacheProvider
  18. {
  19. /** @var MongoCollection */
  20. private $collection;
  21. /** @var bool */
  22. private $expirationIndexCreated = false;
  23. /**
  24. * This provider will default to the write concern and read preference
  25. * options set on the MongoCollection instance (or inherited from MongoDB or
  26. * MongoClient). Using an unacknowledged write concern (< 1) may make the
  27. * return values of delete() and save() unreliable. Reading from secondaries
  28. * may make contain() and fetch() unreliable.
  29. *
  30. * @see http://www.php.net/manual/en/mongo.readpreferences.php
  31. * @see http://www.php.net/manual/en/mongo.writeconcerns.php
  32. */
  33. public function __construct(MongoCollection $collection)
  34. {
  35. @trigger_error('Using the legacy MongoDB cache provider is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
  36. $this->collection = $collection;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function doFetch($id)
  42. {
  43. $document = $this->collection->findOne(['_id' => $id], [MongoDBCache::DATA_FIELD, MongoDBCache::EXPIRATION_FIELD]);
  44. if ($document === null) {
  45. return false;
  46. }
  47. if ($this->isExpired($document)) {
  48. $this->createExpirationIndex();
  49. $this->doDelete($id);
  50. return false;
  51. }
  52. return unserialize($document[MongoDBCache::DATA_FIELD]->bin);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. protected function doContains($id)
  58. {
  59. $document = $this->collection->findOne(['_id' => $id], [MongoDBCache::EXPIRATION_FIELD]);
  60. if ($document === null) {
  61. return false;
  62. }
  63. if ($this->isExpired($document)) {
  64. $this->createExpirationIndex();
  65. $this->doDelete($id);
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function doSave($id, $data, $lifeTime = 0)
  74. {
  75. try {
  76. $result = $this->collection->update(
  77. ['_id' => $id],
  78. [
  79. '$set' => [
  80. MongoDBCache::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null),
  81. MongoDBCache::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY),
  82. ],
  83. ],
  84. ['upsert' => true, 'multiple' => false]
  85. );
  86. } catch (MongoCursorException $e) {
  87. return false;
  88. }
  89. return ($result['ok'] ?? 1) == 1;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. protected function doDelete($id)
  95. {
  96. $result = $this->collection->remove(['_id' => $id]);
  97. return ($result['ok'] ?? 1) == 1;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function doFlush()
  103. {
  104. // Use remove() in lieu of drop() to maintain any collection indexes
  105. $result = $this->collection->remove();
  106. return ($result['ok'] ?? 1) == 1;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function doGetStats()
  112. {
  113. $serverStatus = $this->collection->db->command([
  114. 'serverStatus' => 1,
  115. 'locks' => 0,
  116. 'metrics' => 0,
  117. 'recordStats' => 0,
  118. 'repl' => 0,
  119. ]);
  120. $collStats = $this->collection->db->command(['collStats' => 1]);
  121. return [
  122. Cache::STATS_HITS => null,
  123. Cache::STATS_MISSES => null,
  124. Cache::STATS_UPTIME => $serverStatus['uptime'] ?? null,
  125. Cache::STATS_MEMORY_USAGE => $collStats['size'] ?? null,
  126. Cache::STATS_MEMORY_AVAILABLE => null,
  127. ];
  128. }
  129. /**
  130. * Check if the document is expired.
  131. *
  132. * @param array $document
  133. */
  134. private function isExpired(array $document) : bool
  135. {
  136. return isset($document[MongoDBCache::EXPIRATION_FIELD]) &&
  137. $document[MongoDBCache::EXPIRATION_FIELD] instanceof MongoDate &&
  138. $document[MongoDBCache::EXPIRATION_FIELD]->sec < time();
  139. }
  140. private function createExpirationIndex() : void
  141. {
  142. if ($this->expirationIndexCreated) {
  143. return;
  144. }
  145. $this->expirationIndexCreated = true;
  146. $this->collection->createIndex([MongoDBCache::EXPIRATION_FIELD => 1], ['background' => true, 'expireAfterSeconds' => 0]);
  147. }
  148. }