annotate vendor/doctrine/cache/lib/Doctrine/Common/Cache/MongoDBCache.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Cache;
Chris@0 21
Chris@0 22 use MongoBinData;
Chris@0 23 use MongoCollection;
Chris@0 24 use MongoCursorException;
Chris@0 25 use MongoDate;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * MongoDB cache provider.
Chris@0 29 *
Chris@0 30 * @since 1.1
Chris@0 31 * @author Jeremy Mikola <jmikola@gmail.com>
Chris@0 32 */
Chris@0 33 class MongoDBCache extends CacheProvider
Chris@0 34 {
Chris@0 35 /**
Chris@0 36 * The data field will store the serialized PHP value.
Chris@0 37 */
Chris@0 38 const DATA_FIELD = 'd';
Chris@0 39
Chris@0 40 /**
Chris@0 41 * The expiration field will store a MongoDate value indicating when the
Chris@0 42 * cache entry should expire.
Chris@0 43 *
Chris@0 44 * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by
Chris@0 45 * indexing this field with the "expireAfterSeconds" option equal to zero.
Chris@0 46 * This will direct MongoDB to regularly query for and delete any entries
Chris@0 47 * whose date is older than the current time. Entries without a date value
Chris@0 48 * in this field will be ignored.
Chris@0 49 *
Chris@0 50 * The cache provider will also check dates on its own, in case expired
Chris@0 51 * entries are fetched before MongoDB's TTLMonitor pass can expire them.
Chris@0 52 *
Chris@0 53 * @see http://docs.mongodb.org/manual/tutorial/expire-data/
Chris@0 54 */
Chris@0 55 const EXPIRATION_FIELD = 'e';
Chris@0 56
Chris@0 57 /**
Chris@0 58 * @var MongoCollection
Chris@0 59 */
Chris@0 60 private $collection;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Constructor.
Chris@0 64 *
Chris@0 65 * This provider will default to the write concern and read preference
Chris@0 66 * options set on the MongoCollection instance (or inherited from MongoDB or
Chris@0 67 * MongoClient). Using an unacknowledged write concern (< 1) may make the
Chris@0 68 * return values of delete() and save() unreliable. Reading from secondaries
Chris@0 69 * may make contain() and fetch() unreliable.
Chris@0 70 *
Chris@0 71 * @see http://www.php.net/manual/en/mongo.readpreferences.php
Chris@0 72 * @see http://www.php.net/manual/en/mongo.writeconcerns.php
Chris@0 73 * @param MongoCollection $collection
Chris@0 74 */
Chris@0 75 public function __construct(MongoCollection $collection)
Chris@0 76 {
Chris@0 77 $this->collection = $collection;
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * {@inheritdoc}
Chris@0 82 */
Chris@0 83 protected function doFetch($id)
Chris@0 84 {
Chris@0 85 $document = $this->collection->findOne(array('_id' => $id), array(self::DATA_FIELD, self::EXPIRATION_FIELD));
Chris@0 86
Chris@0 87 if ($document === null) {
Chris@0 88 return false;
Chris@0 89 }
Chris@0 90
Chris@0 91 if ($this->isExpired($document)) {
Chris@0 92 $this->doDelete($id);
Chris@0 93 return false;
Chris@0 94 }
Chris@0 95
Chris@0 96 return unserialize($document[self::DATA_FIELD]->bin);
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * {@inheritdoc}
Chris@0 101 */
Chris@0 102 protected function doContains($id)
Chris@0 103 {
Chris@0 104 $document = $this->collection->findOne(array('_id' => $id), array(self::EXPIRATION_FIELD));
Chris@0 105
Chris@0 106 if ($document === null) {
Chris@0 107 return false;
Chris@0 108 }
Chris@0 109
Chris@0 110 if ($this->isExpired($document)) {
Chris@0 111 $this->doDelete($id);
Chris@0 112 return false;
Chris@0 113 }
Chris@0 114
Chris@0 115 return true;
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 */
Chris@0 121 protected function doSave($id, $data, $lifeTime = 0)
Chris@0 122 {
Chris@0 123 try {
Chris@0 124 $result = $this->collection->update(
Chris@0 125 array('_id' => $id),
Chris@0 126 array('$set' => array(
Chris@0 127 self::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null),
Chris@0 128 self::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY),
Chris@0 129 )),
Chris@0 130 array('upsert' => true, 'multiple' => false)
Chris@0 131 );
Chris@0 132 } catch (MongoCursorException $e) {
Chris@0 133 return false;
Chris@0 134 }
Chris@0 135
Chris@0 136 return isset($result['ok']) ? $result['ok'] == 1 : true;
Chris@0 137 }
Chris@0 138
Chris@0 139 /**
Chris@0 140 * {@inheritdoc}
Chris@0 141 */
Chris@0 142 protected function doDelete($id)
Chris@0 143 {
Chris@0 144 $result = $this->collection->remove(array('_id' => $id));
Chris@0 145
Chris@0 146 return isset($result['ok']) ? $result['ok'] == 1 : true;
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * {@inheritdoc}
Chris@0 151 */
Chris@0 152 protected function doFlush()
Chris@0 153 {
Chris@0 154 // Use remove() in lieu of drop() to maintain any collection indexes
Chris@0 155 $result = $this->collection->remove();
Chris@0 156
Chris@0 157 return isset($result['ok']) ? $result['ok'] == 1 : true;
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 protected function doGetStats()
Chris@0 164 {
Chris@0 165 $serverStatus = $this->collection->db->command(array(
Chris@0 166 'serverStatus' => 1,
Chris@0 167 'locks' => 0,
Chris@0 168 'metrics' => 0,
Chris@0 169 'recordStats' => 0,
Chris@0 170 'repl' => 0,
Chris@0 171 ));
Chris@0 172
Chris@0 173 $collStats = $this->collection->db->command(array('collStats' => 1));
Chris@0 174
Chris@0 175 return array(
Chris@0 176 Cache::STATS_HITS => null,
Chris@0 177 Cache::STATS_MISSES => null,
Chris@0 178 Cache::STATS_UPTIME => (isset($serverStatus['uptime']) ? (int) $serverStatus['uptime'] : null),
Chris@0 179 Cache::STATS_MEMORY_USAGE => (isset($collStats['size']) ? (int) $collStats['size'] : null),
Chris@0 180 Cache::STATS_MEMORY_AVAILABLE => null,
Chris@0 181 );
Chris@0 182 }
Chris@0 183
Chris@0 184 /**
Chris@0 185 * Check if the document is expired.
Chris@0 186 *
Chris@0 187 * @param array $document
Chris@0 188 *
Chris@0 189 * @return bool
Chris@0 190 */
Chris@0 191 private function isExpired(array $document)
Chris@0 192 {
Chris@0 193 return isset($document[self::EXPIRATION_FIELD]) &&
Chris@0 194 $document[self::EXPIRATION_FIELD] instanceof MongoDate &&
Chris@0 195 $document[self::EXPIRATION_FIELD]->sec < time();
Chris@0 196 }
Chris@0 197 }