Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MongoDb.php
Go to the documentation of this file.
1 <?php
11 
13 {
18 
23 
25 
30  protected $_collection = null;
31 
37  protected $_options = [
38  'connection_string' => 'mongodb://localhost:27017', // MongoDB connection string
39  'mongo_options' => [], // MongoDB connection options
40  'db' => '', // Name of a database to be used for cache storage
41  'collection' => 'cache', // Name of a collection to be used for cache storage
42  ];
43 
47  public function __construct(array $options = [])
48  {
49  if (!extension_loaded('mongo') || !version_compare(\Mongo::VERSION, '1.2.11', '>=')) {
51  "At least 1.2.11 version of 'mongo' extension is required for using MongoDb cache backend"
52  );
53  }
54  if (empty($options['db'])) {
55  \Zend_Cache::throwException("'db' option is not specified");
56  }
57  parent::__construct($options);
58  }
59 
65  protected function _getCollection()
66  {
67  if (null === $this->_collection) {
68  $connection = new \Mongo($this->_options['connection_string'], $this->_options['mongo_options']);
69  $database = $connection->selectDB($this->_options['db']);
70  $this->_collection = $database->selectCollection($this->_options['collection']);
71  }
72  return $this->_collection;
73  }
74 
80  public function getIds()
81  {
82  return array_keys(iterator_to_array($this->_getCollection()->find([], ['_id'])));
83  }
84 
90  public function getTags()
91  {
92  $result = $this->_getCollection()->distinct('tags');
93  return $result ?: [];
94  }
95 
104  public function getIdsMatchingTags($tags = [])
105  {
106  $query = $this->_getQueryMatchingTags($tags, self::COMPARISON_MODE_MATCHING_TAG);
107  if (empty($query)) {
108  return [];
109  }
110  $result = $this->_getCollection()->find($query, ['_id']);
111  return array_keys(iterator_to_array($result));
112  }
113 
122  public function getIdsNotMatchingTags($tags = [])
123  {
124  $query = $this->_getQueryMatchingTags($tags, self::COMPARISON_MODE_NOT_MATCHING_TAG);
125  if (empty($query)) {
126  return [];
127  }
128  $result = $this->_getCollection()->find($query, ['_id']);
129  return array_keys(iterator_to_array($result));
130  }
131 
140  public function getIdsMatchingAnyTags($tags = [])
141  {
142  $query = $this->_getQueryMatchingTags($tags, self::COMPARISON_MODE_MATCHING_ANY_TAG);
143  if (empty($query)) {
144  return [];
145  }
146  $result = $this->_getCollection()->find($query, ['_id']);
147  return array_keys(iterator_to_array($result));
148  }
149 
157  protected function _getQueryMatchingTags(array $tags, $comparisonMode)
158  {
159  $operators = [
160  self::COMPARISON_MODE_MATCHING_TAG => '$and',
161  self::COMPARISON_MODE_NOT_MATCHING_TAG => '$nor',
162  self::COMPARISON_MODE_MATCHING_ANY_TAG => '$or',
163  ];
164  if (!isset($operators[$comparisonMode])) {
165  \Zend_Cache::throwException("Incorrect comparison mode specified: {$comparisonMode}");
166  }
167  $operator = $operators[$comparisonMode];
168  $query = [];
169  foreach ($tags as $tag) {
170  $query[$operator][] = ['tags' => $this->_quoteString($tag)];
171  }
172  return $query;
173  }
174 
181  public function getFillingPercentage()
182  {
183  return 1;
184  }
185 
197  public function getMetadatas($cacheId)
198  {
199  $result = $this->_getCollection()->findOne(
200  ['_id' => $this->_quoteString($cacheId)],
201  ['expire', 'tags', 'mtime']
202  );
203  return $result === null ? false : $result;
204  }
205 
213  public function touch($cacheId, $extraLifetime)
214  {
215  $time = time();
216  $condition = ['_id' => $this->_quoteString($cacheId), 'expire' => ['$gt' => $time]];
217  $update = ['$set' => ['mtime' => $time], '$inc' => ['expire' => (int)$extraLifetime]];
218  return $this->_getCollection()->update($condition, $update);
219  }
220 
235  public function getCapabilities()
236  {
237  return [
238  'automatic_cleaning' => true,
239  'tags' => true,
240  'expired_read' => true,
241  'priority' => false,
242  'infinite_lifetime' => true,
243  'get_list' => true
244  ];
245  }
246 
256  public function load($cacheId, $notTestCacheValidity = false)
257  {
258  $query = ['_id' => $this->_quoteString($cacheId)];
259  if (!$notTestCacheValidity) {
260  $query['$or'] = [
261  ['expire' => self::EXPIRATION_TIME_INFINITE],
262  ['expire' => ['$gt' => time()]],
263  ];
264  }
265  $result = $this->_getCollection()->findOne($query, ['data']);
266  return $result ? $result['data']->bin : false;
267  }
268 
275  public function test($cacheId)
276  {
277  $result = $this->_getCollection()->findOne(
278  [
279  '_id' => $this->_quoteString($cacheId),
280  '$or' => [
281  ['expire' => self::EXPIRATION_TIME_INFINITE],
282  ['expire' => ['$gt' => time()]],
283  ],
284  ],
285  ['mtime']
286  );
287  return $result ? $result['mtime'] : false;
288  }
289 
302  public function save($data, $cacheId, $tags = [], $specificLifetime = false)
303  {
304  $lifetime = $this->getLifetime($specificLifetime);
305  $time = time();
306  $expire = $lifetime === null ? self::EXPIRATION_TIME_INFINITE : $time + $lifetime;
307  $tags = array_map([$this, '_quoteString'], $tags);
308  $document = [
309  '_id' => $this->_quoteString($cacheId),
310  'data' => new \MongoBinData($this->_quoteString($data), \MongoBinData::BYTE_ARRAY),
311  'tags' => $tags,
312  'mtime' => $time,
313  'expire' => $expire,
314  ];
315  return $this->_getCollection()->save($document);
316  }
317 
324  public function remove($cacheId)
325  {
326  return $this->_getCollection()->remove(['_id' => $this->_quoteString($cacheId)]);
327  }
328 
346  public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, $tags = [])
347  {
348  $result = false;
349  switch ($mode) {
350  case \Zend_Cache::CLEANING_MODE_ALL:
351  $result = $this->_getCollection()->drop();
352  $result = (bool)$result['ok'];
353  break;
354  case \Zend_Cache::CLEANING_MODE_OLD:
355  $query = ['expire' => ['$ne' => self::EXPIRATION_TIME_INFINITE, '$lte' => time()]];
356  break;
357  case \Zend_Cache::CLEANING_MODE_MATCHING_TAG:
358  case \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
359  case \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
360  $query = $this->_getQueryMatchingTags((array)$tags, $mode);
361  break;
362  default:
363  \Zend_Cache::throwException('Unsupported cleaning mode: ' . $mode);
364  }
365  if (!empty($query)) {
366  $result = $this->_getCollection()->remove($query);
367  }
368 
369  return $result;
370  }
371 
378  protected function _quoteString($value)
379  {
380  return (string)$value;
381  }
382 }
return false
Definition: gallery.phtml:36
taxRateField find('.mselect-list') .on( 'click.mselect-edit'
Definition: edit.phtml:162
clean($mode=\Zend_Cache::CLEANING_MODE_ALL, $tags=[])
Definition: MongoDb.php:346
load($cacheId, $notTestCacheValidity=false)
Definition: MongoDb.php:256
save($data, $cacheId, $tags=[], $specificLifetime=false)
Definition: MongoDb.php:302
$value
Definition: gender.phtml:16
$this _collection
Definition: coupons.php:7
_getQueryMatchingTags(array $tags, $comparisonMode)
Definition: MongoDb.php:157
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
const CLEANING_MODE_ALL
Definition: Cache.php:72
static throwException($msg, Exception $e=null)
Definition: Cache.php:205
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74
$connection
Definition: bulk.php:13
getLifetime($specificLifetime)
Definition: Backend.php:143
touch($cacheId, $extraLifetime)
Definition: MongoDb.php:213