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.
31 * @author Jeremy Mikola <jmikola@gmail.com>
33 class MongoDBCache extends CacheProvider
36 * The data field will store the serialized PHP value.
38 const DATA_FIELD = 'd';
41 * The expiration field will store a MongoDate value indicating when the
42 * cache entry should expire.
44 * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by
45 * indexing this field with the "expireAfterSeconds" option equal to zero.
46 * This will direct MongoDB to regularly query for and delete any entries
47 * whose date is older than the current time. Entries without a date value
48 * in this field will be ignored.
50 * The cache provider will also check dates on its own, in case expired
51 * entries are fetched before MongoDB's TTLMonitor pass can expire them.
53 * @see http://docs.mongodb.org/manual/tutorial/expire-data/
55 const EXPIRATION_FIELD = 'e';
58 * @var MongoCollection
65 * This provider will default to the write concern and read preference
66 * options set on the MongoCollection instance (or inherited from MongoDB or
67 * MongoClient). Using an unacknowledged write concern (< 1) may make the
68 * return values of delete() and save() unreliable. Reading from secondaries
69 * may make contain() and fetch() unreliable.
71 * @see http://www.php.net/manual/en/mongo.readpreferences.php
72 * @see http://www.php.net/manual/en/mongo.writeconcerns.php
73 * @param MongoCollection $collection
75 public function __construct(MongoCollection $collection)
77 $this->collection = $collection;
83 protected function doFetch($id)
85 $document = $this->collection->findOne(array('_id' => $id), array(self::DATA_FIELD, self::EXPIRATION_FIELD));
87 if ($document === null) {
91 if ($this->isExpired($document)) {
96 return unserialize($document[self::DATA_FIELD]->bin);
102 protected function doContains($id)
104 $document = $this->collection->findOne(array('_id' => $id), array(self::EXPIRATION_FIELD));
106 if ($document === null) {
110 if ($this->isExpired($document)) {
111 $this->doDelete($id);
121 protected function doSave($id, $data, $lifeTime = 0)
124 $result = $this->collection->update(
126 array('$set' => array(
127 self::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null),
128 self::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY),
130 array('upsert' => true, 'multiple' => false)
132 } catch (MongoCursorException $e) {
136 return isset($result['ok']) ? $result['ok'] == 1 : true;
142 protected function doDelete($id)
144 $result = $this->collection->remove(array('_id' => $id));
146 return isset($result['ok']) ? $result['ok'] == 1 : true;
152 protected function doFlush()
154 // Use remove() in lieu of drop() to maintain any collection indexes
155 $result = $this->collection->remove();
157 return isset($result['ok']) ? $result['ok'] == 1 : true;
163 protected function doGetStats()
165 $serverStatus = $this->collection->db->command(array(
173 $collStats = $this->collection->db->command(array('collStats' => 1));
176 Cache::STATS_HITS => null,
177 Cache::STATS_MISSES => null,
178 Cache::STATS_UPTIME => (isset($serverStatus['uptime']) ? (int) $serverStatus['uptime'] : null),
179 Cache::STATS_MEMORY_USAGE => (isset($collStats['size']) ? (int) $collStats['size'] : null),
180 Cache::STATS_MEMORY_AVAILABLE => null,
185 * Check if the document is expired.
187 * @param array $document
191 private function isExpired(array $document)
193 return isset($document[self::EXPIRATION_FIELD]) &&
194 $document[self::EXPIRATION_FIELD] instanceof MongoDate &&
195 $document[self::EXPIRATION_FIELD]->sec < time();