MongoDBCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use InvalidArgumentException;
  4. use MongoCollection;
  5. use MongoDB\Collection;
  6. use const E_USER_DEPRECATED;
  7. use function trigger_error;
  8. /**
  9. * MongoDB cache provider.
  10. */
  11. class MongoDBCache extends CacheProvider
  12. {
  13. /**
  14. * The data field will store the serialized PHP value.
  15. */
  16. public const DATA_FIELD = 'd';
  17. /**
  18. * The expiration field will store a MongoDate value indicating when the
  19. * cache entry should expire.
  20. *
  21. * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by
  22. * indexing this field with the "expireAfterSeconds" option equal to zero.
  23. * This will direct MongoDB to regularly query for and delete any entries
  24. * whose date is older than the current time. Entries without a date value
  25. * in this field will be ignored.
  26. *
  27. * The cache provider will also check dates on its own, in case expired
  28. * entries are fetched before MongoDB's TTLMonitor pass can expire them.
  29. *
  30. * @see http://docs.mongodb.org/manual/tutorial/expire-data/
  31. */
  32. public const EXPIRATION_FIELD = 'e';
  33. /** @var CacheProvider */
  34. private $provider;
  35. /**
  36. * This provider will default to the write concern and read preference
  37. * options set on the collection instance (or inherited from MongoDB or
  38. * MongoClient). Using an unacknowledged write concern (< 1) may make the
  39. * return values of delete() and save() unreliable. Reading from secondaries
  40. * may make contain() and fetch() unreliable.
  41. *
  42. * @see http://www.php.net/manual/en/mongo.readpreferences.php
  43. * @see http://www.php.net/manual/en/mongo.writeconcerns.php
  44. *
  45. * @param MongoCollection|Collection $collection
  46. */
  47. public function __construct($collection)
  48. {
  49. if ($collection instanceof MongoCollection) {
  50. @trigger_error('Using a MongoCollection instance for creating a cache adapter is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
  51. $this->provider = new LegacyMongoDBCache($collection);
  52. } elseif ($collection instanceof Collection) {
  53. $this->provider = new ExtMongoDBCache($collection);
  54. } else {
  55. throw new InvalidArgumentException('Invalid collection given - expected a MongoCollection or MongoDB\Collection instance');
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function doFetch($id)
  62. {
  63. return $this->provider->doFetch($id);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function doContains($id)
  69. {
  70. return $this->provider->doContains($id);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function doSave($id, $data, $lifeTime = 0)
  76. {
  77. return $this->provider->doSave($id, $data, $lifeTime);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function doDelete($id)
  83. {
  84. return $this->provider->doDelete($id);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function doFlush()
  90. {
  91. return $this->provider->doFlush();
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function doGetStats()
  97. {
  98. return $this->provider->doGetStats();
  99. }
  100. }