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;
23 use MongoDB\Collection;
26 * MongoDB cache provider.
29 * @author Jeremy Mikola <jmikola@gmail.com>
31 class MongoDBCache extends CacheProvider
34 * The data field will store the serialized PHP value.
36 const DATA_FIELD = 'd';
39 * The expiration field will store a MongoDate value indicating when the
40 * cache entry should expire.
42 * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by
43 * indexing this field with the "expireAfterSeconds" option equal to zero.
44 * This will direct MongoDB to regularly query for and delete any entries
45 * whose date is older than the current time. Entries without a date value
46 * in this field will be ignored.
48 * The cache provider will also check dates on its own, in case expired
49 * entries are fetched before MongoDB's TTLMonitor pass can expire them.
51 * @see http://docs.mongodb.org/manual/tutorial/expire-data/
53 const EXPIRATION_FIELD = 'e';
63 * This provider will default to the write concern and read preference
64 * options set on the collection instance (or inherited from MongoDB or
65 * MongoClient). Using an unacknowledged write concern (< 1) may make the
66 * return values of delete() and save() unreliable. Reading from secondaries
67 * may make contain() and fetch() unreliable.
69 * @see http://www.php.net/manual/en/mongo.readpreferences.php
70 * @see http://www.php.net/manual/en/mongo.writeconcerns.php
71 * @param MongoCollection|Collection $collection
73 public function __construct($collection)
75 if ($collection instanceof MongoCollection) {
76 @trigger_error('Using a MongoCollection instance for creating a cache adapter is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
77 $this->provider = new LegacyMongoDBCache($collection);
78 } elseif ($collection instanceof Collection) {
79 $this->provider = new ExtMongoDBCache($collection);
81 throw new \InvalidArgumentException('Invalid collection given - expected a MongoCollection or MongoDB\Collection instance');
88 protected function doFetch($id)
90 return $this->provider->doFetch($id);
96 protected function doContains($id)
98 return $this->provider->doContains($id);
104 protected function doSave($id, $data, $lifeTime = 0)
106 return $this->provider->doSave($id, $data, $lifeTime);
112 protected function doDelete($id)
114 return $this->provider->doDelete($id);
120 protected function doFlush()
122 return $this->provider->doFlush();
128 protected function doGetStats()
130 return $this->provider->doGetStats();