MongoDBCache.php 3.1 KB

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