MongoDBCache.php 3.2 KB

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