3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Cache;
24 use MongoCursorException;
28 * MongoDB cache provider.
30 * @author Jeremy Mikola <jmikola@gmail.com>
31 * @internal Do not use - will be removed in 2.0. Use MongoDBCache instead
33 class LegacyMongoDBCache extends CacheProvider
36 * @var MongoCollection
43 private $expirationIndexCreated = false;
48 * This provider will default to the write concern and read preference
49 * options set on the MongoCollection instance (or inherited from MongoDB or
50 * MongoClient). Using an unacknowledged write concern (< 1) may make the
51 * return values of delete() and save() unreliable. Reading from secondaries
52 * may make contain() and fetch() unreliable.
54 * @see http://www.php.net/manual/en/mongo.readpreferences.php
55 * @see http://www.php.net/manual/en/mongo.writeconcerns.php
56 * @param MongoCollection $collection
58 public function __construct(MongoCollection $collection)
60 @trigger_error('Using the legacy MongoDB cache provider is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
61 $this->collection = $collection;
67 protected function doFetch($id)
69 $document = $this->collection->findOne(['_id' => $id], [MongoDBCache::DATA_FIELD, MongoDBCache::EXPIRATION_FIELD]);
71 if ($document === null) {
75 if ($this->isExpired($document)) {
76 $this->createExpirationIndex();
81 return unserialize($document[MongoDBCache::DATA_FIELD]->bin);
87 protected function doContains($id)
89 $document = $this->collection->findOne(['_id' => $id], [MongoDBCache::EXPIRATION_FIELD]);
91 if ($document === null) {
95 if ($this->isExpired($document)) {
96 $this->createExpirationIndex();
107 protected function doSave($id, $data, $lifeTime = 0)
110 $result = $this->collection->update(
113 MongoDBCache::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null),
114 MongoDBCache::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY),
116 ['upsert' => true, 'multiple' => false]
118 } catch (MongoCursorException $e) {
122 return ($result['ok'] ?? 1) == 1;
128 protected function doDelete($id)
130 $result = $this->collection->remove(['_id' => $id]);
132 return ($result['ok'] ?? 1) == 1;
138 protected function doFlush()
140 // Use remove() in lieu of drop() to maintain any collection indexes
141 $result = $this->collection->remove();
143 return ($result['ok'] ?? 1) == 1;
149 protected function doGetStats()
151 $serverStatus = $this->collection->db->command([
159 $collStats = $this->collection->db->command(['collStats' => 1]);
162 Cache::STATS_HITS => null,
163 Cache::STATS_MISSES => null,
164 Cache::STATS_UPTIME => $serverStatus['uptime'] ?? null,
165 Cache::STATS_MEMORY_USAGE => $collStats['size'] ?? null,
166 Cache::STATS_MEMORY_AVAILABLE => null,
171 * Check if the document is expired.
173 * @param array $document
177 private function isExpired(array $document) : bool
179 return isset($document[MongoDBCache::EXPIRATION_FIELD]) &&
180 $document[MongoDBCache::EXPIRATION_FIELD] instanceof MongoDate &&
181 $document[MongoDBCache::EXPIRATION_FIELD]->sec < time();
185 private function createExpirationIndex(): void
187 if ($this->expirationIndexCreated) {
191 $this->expirationIndexCreated = true;
192 $this->collection->createIndex([MongoDBCache::EXPIRATION_FIELD => 1], ['background' => true, 'expireAfterSeconds' => 0]);