MongoDb.php 12.9 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * MongoDb cache backend
 */
namespace Magento\Framework\Cache\Backend;

class MongoDb extends \Zend_Cache_Backend implements \Zend_Cache_Backend_ExtendedInterface
{
    /**
     * Infinite expiration time
     */
    const EXPIRATION_TIME_INFINITE = 0;

    /**#@+
     * Available comparison modes. Used for composing queries to search by tags
     */
    const COMPARISON_MODE_MATCHING_TAG = \Zend_Cache::CLEANING_MODE_MATCHING_TAG;

    const COMPARISON_MODE_NOT_MATCHING_TAG = \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG;

    const COMPARISON_MODE_MATCHING_ANY_TAG = \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG;
    /**#@-*/

    /**#@-*/
    protected $_collection = null;

    /**
     * List of available options
     *
     * @var array
     */
    protected $_options = [
        'connection_string' => 'mongodb://localhost:27017', // MongoDB connection string
        'mongo_options' => [], // MongoDB connection options
        'db' => '', // Name of a database to be used for cache storage
        'collection' => 'cache', // Name of a collection to be used for cache storage
    ];

    /**
     * @param array $options
     */
    public function __construct(array $options = [])
    {
        if (!extension_loaded('mongo') || !version_compare(\Mongo::VERSION, '1.2.11', '>=')) {
            \Zend_Cache::throwException(
                "At least 1.2.11 version of 'mongo' extension is required for using MongoDb cache backend"
            );
        }
        if (empty($options['db'])) {
            \Zend_Cache::throwException("'db' option is not specified");
        }
        parent::__construct($options);
    }

    /**
     * Get collection
     *
     * @return \MongoCollection
     */
    protected function _getCollection()
    {
        if (null === $this->_collection) {
            $connection = new \Mongo($this->_options['connection_string'], $this->_options['mongo_options']);
            $database = $connection->selectDB($this->_options['db']);
            $this->_collection = $database->selectCollection($this->_options['collection']);
        }
        return $this->_collection;
    }

    /**
     * Return an array of stored cache ids
     *
     * @return string[] array of stored cache ids (string)
     */
    public function getIds()
    {
        return array_keys(iterator_to_array($this->_getCollection()->find([], ['_id'])));
    }

    /**
     * Return an array of stored tags
     *
     * @return string[] array of stored tags (string)
     */
    public function getTags()
    {
        $result = $this->_getCollection()->distinct('tags');
        return $result ?: [];
    }

    /**
     * Return an array of stored cache ids which match given tags
     *
     * In case of multiple tags, a logical AND is made between tags
     *
     * @param string[] $tags array of tags
     * @return string[] array of matching cache ids (string)
     */
    public function getIdsMatchingTags($tags = [])
    {
        $query = $this->_getQueryMatchingTags($tags, self::COMPARISON_MODE_MATCHING_TAG);
        if (empty($query)) {
            return [];
        }
        $result = $this->_getCollection()->find($query, ['_id']);
        return array_keys(iterator_to_array($result));
    }

    /**
     * Return an array of stored cache ids which don't match given tags
     *
     * In case of multiple tags, a logical OR is made between tags
     *
     * @param string[] $tags array of tags
     * @return string[] array of not matching cache ids (string)
     */
    public function getIdsNotMatchingTags($tags = [])
    {
        $query = $this->_getQueryMatchingTags($tags, self::COMPARISON_MODE_NOT_MATCHING_TAG);
        if (empty($query)) {
            return [];
        }
        $result = $this->_getCollection()->find($query, ['_id']);
        return array_keys(iterator_to_array($result));
    }

    /**
     * Return an array of stored cache ids which match any given tags
     *
     * In case of multiple tags, a logical AND is made between tags
     *
     * @param string[] $tags array of tags
     * @return string[] array of any matching cache ids (string)
     */
    public function getIdsMatchingAnyTags($tags = [])
    {
        $query = $this->_getQueryMatchingTags($tags, self::COMPARISON_MODE_MATCHING_ANY_TAG);
        if (empty($query)) {
            return [];
        }
        $result = $this->_getCollection()->find($query, ['_id']);
        return array_keys(iterator_to_array($result));
    }

    /**
     * Get query to filter by specified tags and comparison mode
     *
     * @param string[] $tags
     * @param string $comparisonMode
     * @return array
     */
    protected function _getQueryMatchingTags(array $tags, $comparisonMode)
    {
        $operators = [
            self::COMPARISON_MODE_MATCHING_TAG => '$and',
            self::COMPARISON_MODE_NOT_MATCHING_TAG => '$nor',
            self::COMPARISON_MODE_MATCHING_ANY_TAG => '$or',
        ];
        if (!isset($operators[$comparisonMode])) {
            \Zend_Cache::throwException("Incorrect comparison mode specified: {$comparisonMode}");
        }
        $operator = $operators[$comparisonMode];
        $query = [];
        foreach ($tags as $tag) {
            $query[$operator][] = ['tags' => $this->_quoteString($tag)];
        }
        return $query;
    }

    /**
     * Return the filling percentage of the backend storage
     *
     * @return int integer between 0 and 100
     * TODO: implement basing on info from MongoDB server
     */
    public function getFillingPercentage()
    {
        return 1;
    }

    /**
     * Return an array of metadatas for the given cache id
     *
     * The array must include these keys :
     * - expire : the expire timestamp
     * - tags : a string array of tags
     * - mtime : timestamp of last modification time
     *
     * @param string $cacheId cache id
     * @return array|false array of metadatas (false if the cache id is not found)
     */
    public function getMetadatas($cacheId)
    {
        $result = $this->_getCollection()->findOne(
            ['_id' => $this->_quoteString($cacheId)],
            ['expire', 'tags', 'mtime']
        );
        return $result === null ? false : $result;
    }

    /**
     * Give (if possible) an extra lifetime to the given cache id
     *
     * @param string $cacheId cache id
     * @param int $extraLifetime
     * @return boolean true if ok
     */
    public function touch($cacheId, $extraLifetime)
    {
        $time = time();
        $condition = ['_id' => $this->_quoteString($cacheId), 'expire' => ['$gt' => $time]];
        $update = ['$set' => ['mtime' => $time], '$inc' => ['expire' => (int)$extraLifetime]];
        return $this->_getCollection()->update($condition, $update);
    }

    /**
     * Return an associative array of capabilities (booleans) of the backend
     *
     * The array must include these keys :
     * - automatic_cleaning (is automating cleaning necessary)
     * - tags (are tags supported)
     * - expired_read (is it possible to read expired cache records
     *                 (for doNotTestCacheValidity option for example))
     * - priority does the backend deal with priority when saving
     * - infinite_lifetime (is infinite lifetime can work with this backend)
     * - get_list (is it possible to get the list of cache ids and the complete list of tags)
     *
     * @return array associative of with capabilities
     */
    public function getCapabilities()
    {
        return [
            'automatic_cleaning' => true,
            'tags' => true,
            'expired_read' => true,
            'priority' => false,
            'infinite_lifetime' => true,
            'get_list' => true
        ];
    }

    /**
     * Test if a cache is available for the given id and (if yes) return it (false else)
     *
     * Note : return value is always "string" (unserialization is done by the core not by the backend)
     *
     * @param  string  $cacheId                     Cache id
     * @param  boolean $notTestCacheValidity If set to true, the cache validity won't be tested
     * @return string|bool cached data. Return false if nothing found
     */
    public function load($cacheId, $notTestCacheValidity = false)
    {
        $query = ['_id' => $this->_quoteString($cacheId)];
        if (!$notTestCacheValidity) {
            $query['$or'] = [
                ['expire' => self::EXPIRATION_TIME_INFINITE],
                ['expire' => ['$gt' => time()]],
            ];
        }
        $result = $this->_getCollection()->findOne($query, ['data']);
        return $result ? $result['data']->bin : false;
    }

    /**
     * Test if a cache is available or not (for the given id)
     *
     * @param  string $cacheId cache id
     * @return int|bool "last modified" timestamp of the available cache record or false if cache is not available
     */
    public function test($cacheId)
    {
        $result = $this->_getCollection()->findOne(
            [
                '_id' => $this->_quoteString($cacheId),
                '$or' => [
                    ['expire' => self::EXPIRATION_TIME_INFINITE],
                    ['expire' => ['$gt' => time()]],
                ],
            ],
            ['mtime']
        );
        return $result ? $result['mtime'] : false;
    }

    /**
     * Save some string data into a cache record
     *
     * Note : $data is always "string" (serialization is done by the
     * core not by the backend)
     *
     * @param  string $data            Datas to cache
     * @param  string $cacheId              Cache id
     * @param  string[] $tags             Array of strings, the cache record will be tagged by each string entry
     * @param  int|bool $specificLifetime If != false, set a specific lifetime (null => infinite lifetime)
     * @return boolean true if no problem
     */
    public function save($data, $cacheId, $tags = [], $specificLifetime = false)
    {
        $lifetime = $this->getLifetime($specificLifetime);
        $time = time();
        $expire = $lifetime === null ? self::EXPIRATION_TIME_INFINITE : $time + $lifetime;
        $tags = array_map([$this, '_quoteString'], $tags);
        $document = [
            '_id' => $this->_quoteString($cacheId),
            'data' => new \MongoBinData($this->_quoteString($data), \MongoBinData::BYTE_ARRAY),
            'tags' => $tags,
            'mtime' => $time,
            'expire' => $expire,
        ];
        return $this->_getCollection()->save($document);
    }

    /**
     * Remove a cache record
     *
     * @param  string $cacheId Cache id
     * @return boolean True if no problem
     */
    public function remove($cacheId)
    {
        return $this->_getCollection()->remove(['_id' => $this->_quoteString($cacheId)]);
    }

    /**
     * Clean some cache records
     *
     * Available modes are :
     * \Zend_Cache::CLEANING_MODE_ALL (default)    => remove all cache entries ($tags is not used)
     * \Zend_Cache::CLEANING_MODE_OLD              => remove too old cache entries ($tags is not used)
     * \Zend_Cache::CLEANING_MODE_MATCHING_TAG     => remove cache entries matching all given tags
     *                                               ($tags can be an array of strings or a single string)
     * \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
     *                                               ($tags can be an array of strings or a single string)
     * \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
     *                                               ($tags can be an array of strings or a single string)
     *
     * @param  string $mode Clean mode
     * @param  string[] $tags Array of tags
     * @return bool true if no problem
     */
    public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
    {
        $result = false;
        switch ($mode) {
            case \Zend_Cache::CLEANING_MODE_ALL:
                $result = $this->_getCollection()->drop();
                $result = (bool)$result['ok'];
                break;
            case \Zend_Cache::CLEANING_MODE_OLD:
                $query = ['expire' => ['$ne' => self::EXPIRATION_TIME_INFINITE, '$lte' => time()]];
                break;
            case \Zend_Cache::CLEANING_MODE_MATCHING_TAG:
            case \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
            case \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
                $query = $this->_getQueryMatchingTags((array)$tags, $mode);
                break;
            default:
                \Zend_Cache::throwException('Unsupported cleaning mode: ' . $mode);
        }
        if (!empty($query)) {
            $result = $this->_getCollection()->remove($query);
        }

        return $result;
    }

    /**
     * Quote specified value to be used in query as string
     *
     * @param string $value
     * @return string
     */
    protected function _quoteString($value)
    {
        return (string)$value;
    }
}