Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common\Cache; Chris@0: Chris@0: use MongoBinData; Chris@0: use MongoCollection; Chris@0: use MongoCursorException; Chris@0: use MongoDate; Chris@0: Chris@0: /** Chris@0: * MongoDB cache provider. Chris@0: * Chris@0: * @since 1.1 Chris@0: * @author Jeremy Mikola Chris@0: */ Chris@0: class MongoDBCache extends CacheProvider Chris@0: { Chris@0: /** Chris@0: * The data field will store the serialized PHP value. Chris@0: */ Chris@0: const DATA_FIELD = 'd'; Chris@0: Chris@0: /** Chris@0: * The expiration field will store a MongoDate value indicating when the Chris@0: * cache entry should expire. Chris@0: * Chris@0: * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by Chris@0: * indexing this field with the "expireAfterSeconds" option equal to zero. Chris@0: * This will direct MongoDB to regularly query for and delete any entries Chris@0: * whose date is older than the current time. Entries without a date value Chris@0: * in this field will be ignored. Chris@0: * Chris@0: * The cache provider will also check dates on its own, in case expired Chris@0: * entries are fetched before MongoDB's TTLMonitor pass can expire them. Chris@0: * Chris@0: * @see http://docs.mongodb.org/manual/tutorial/expire-data/ Chris@0: */ Chris@0: const EXPIRATION_FIELD = 'e'; Chris@0: Chris@0: /** Chris@0: * @var MongoCollection Chris@0: */ Chris@0: private $collection; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * This provider will default to the write concern and read preference Chris@0: * options set on the MongoCollection instance (or inherited from MongoDB or Chris@0: * MongoClient). Using an unacknowledged write concern (< 1) may make the Chris@0: * return values of delete() and save() unreliable. Reading from secondaries Chris@0: * may make contain() and fetch() unreliable. Chris@0: * Chris@0: * @see http://www.php.net/manual/en/mongo.readpreferences.php Chris@0: * @see http://www.php.net/manual/en/mongo.writeconcerns.php Chris@0: * @param MongoCollection $collection Chris@0: */ Chris@0: public function __construct(MongoCollection $collection) Chris@0: { Chris@0: $this->collection = $collection; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doFetch($id) Chris@0: { Chris@0: $document = $this->collection->findOne(array('_id' => $id), array(self::DATA_FIELD, self::EXPIRATION_FIELD)); Chris@0: Chris@0: if ($document === null) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if ($this->isExpired($document)) { Chris@0: $this->doDelete($id); Chris@0: return false; Chris@0: } Chris@0: Chris@0: return unserialize($document[self::DATA_FIELD]->bin); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doContains($id) Chris@0: { Chris@0: $document = $this->collection->findOne(array('_id' => $id), array(self::EXPIRATION_FIELD)); Chris@0: Chris@0: if ($document === null) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if ($this->isExpired($document)) { Chris@0: $this->doDelete($id); Chris@0: return false; Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doSave($id, $data, $lifeTime = 0) Chris@0: { Chris@0: try { Chris@0: $result = $this->collection->update( Chris@0: array('_id' => $id), Chris@0: array('$set' => array( Chris@0: self::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null), Chris@0: self::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY), Chris@0: )), Chris@0: array('upsert' => true, 'multiple' => false) Chris@0: ); Chris@0: } catch (MongoCursorException $e) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return isset($result['ok']) ? $result['ok'] == 1 : true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doDelete($id) Chris@0: { Chris@0: $result = $this->collection->remove(array('_id' => $id)); Chris@0: Chris@0: return isset($result['ok']) ? $result['ok'] == 1 : true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doFlush() Chris@0: { Chris@0: // Use remove() in lieu of drop() to maintain any collection indexes Chris@0: $result = $this->collection->remove(); Chris@0: Chris@0: return isset($result['ok']) ? $result['ok'] == 1 : true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doGetStats() Chris@0: { Chris@0: $serverStatus = $this->collection->db->command(array( Chris@0: 'serverStatus' => 1, Chris@0: 'locks' => 0, Chris@0: 'metrics' => 0, Chris@0: 'recordStats' => 0, Chris@0: 'repl' => 0, Chris@0: )); Chris@0: Chris@0: $collStats = $this->collection->db->command(array('collStats' => 1)); Chris@0: Chris@0: return array( Chris@0: Cache::STATS_HITS => null, Chris@0: Cache::STATS_MISSES => null, Chris@0: Cache::STATS_UPTIME => (isset($serverStatus['uptime']) ? (int) $serverStatus['uptime'] : null), Chris@0: Cache::STATS_MEMORY_USAGE => (isset($collStats['size']) ? (int) $collStats['size'] : null), Chris@0: Cache::STATS_MEMORY_AVAILABLE => null, Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check if the document is expired. Chris@0: * Chris@0: * @param array $document Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: private function isExpired(array $document) Chris@0: { Chris@0: return isset($document[self::EXPIRATION_FIELD]) && Chris@0: $document[self::EXPIRATION_FIELD] instanceof MongoDate && Chris@0: $document[self::EXPIRATION_FIELD]->sec < time(); Chris@0: } Chris@0: }