Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Manager.php
Go to the documentation of this file.
1 <?php
23 #require_once 'Zend/Memory/Container/Movable.php';
24 
26 #require_once 'Zend/Memory/Container/Locked.php';
27 
29 #require_once 'Zend/Memory/AccessController.php';
30 
31 
45 {
51  private $_backend = null;
52 
60  private $_memoryLimit = -1;
61 
69  private $_minSize = 16384;
70 
76  private $_memorySize = 0;
77 
83  private $_nextId = 0;
84 
97  private $_unloadCandidates = array();
98 
108  private $_sizes = array();
109 
120  private $_lastModified = null;
121 
127  private $_managerId;
128 
134  private $_tags;
135 
139  private function _generateMemManagerId()
140  {
147  $this->_managerId = uniqid('ZendMemManager', true);
148  $this->_tags = array($this->_managerId);
149  $this->_managerId .= '_';
150  }
151 
152 
161  public function __construct($backend = null)
162  {
163  if ($backend === null) {
164  return;
165  }
166 
167  $this->_backend = $backend;
168  $this->_generateMemManagerId();
169 
170  $memoryLimitStr = trim(ini_get('memory_limit'));
171  if ($memoryLimitStr != '' && $memoryLimitStr != -1) {
172  $this->_memoryLimit = (integer)$memoryLimitStr;
173  switch (strtolower($memoryLimitStr[strlen($memoryLimitStr)-1])) {
174  case 'g':
175  $this->_memoryLimit *= 1024;
176  // Break intentionally omitted
177  case 'm':
178  $this->_memoryLimit *= 1024;
179  // Break intentionally omitted
180  case 'k':
181  $this->_memoryLimit *= 1024;
182  break;
183 
184  default:
185  break;
186  }
187 
188  $this->_memoryLimit = (int)($this->_memoryLimit*2/3);
189  } // No limit otherwise
190  }
191 
197  public function __destruct()
198  {
199  if ($this->_backend !== null) {
200  $this->_backend->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $this->_tags);
201  }
202  }
203 
210  public function setMemoryLimit($newLimit)
211  {
212  $this->_memoryLimit = $newLimit;
213 
214  $this->_swapCheck();
215  }
216 
222  public function getMemoryLimit()
223  {
224  return $this->_memoryLimit;
225  }
226 
232  public function setMinSize($newSize)
233  {
234  $this->_minSize = $newSize;
235  }
236 
242  public function getMinSize()
243  {
244  return $this->_minSize;
245  }
246 
254  public function create($value = '')
255  {
256  return $this->_create($value, false);
257  }
258 
267  public function createLocked($value = '')
268  {
269  return $this->_create($value, true);
270  }
271 
280  private function _create($value, $locked)
281  {
282  $id = $this->_nextId++;
283 
284  if ($locked || ($this->_backend === null) /* Use only memory locked objects if backend is not specified */) {
286  }
287 
288  // Commit other objects modifications
289  $this->_commit();
290 
291  $valueObject = new Zend_Memory_Container_Movable($this, $id, $value);
292 
293  // Store last object size as 0
294  $this->_sizes[$id] = 0;
295  // prepare object for next modifications
296  $this->_lastModified = $valueObject;
297 
298  return new Zend_Memory_AccessController($valueObject);
299  }
300 
310  public function unlink(Zend_Memory_Container_Movable $container, $id)
311  {
312  if ($this->_lastModified === $container) {
313  // Drop all object modifications
314  $this->_lastModified = null;
315  unset($this->_sizes[$id]);
316  return;
317  }
318 
319  if (isset($this->_unloadCandidates[$id])) {
320  unset($this->_unloadCandidates[$id]);
321  }
322 
323  $this->_memorySize -= $this->_sizes[$id];
324  unset($this->_sizes[$id]);
325  }
326 
334  public function processUpdate(Zend_Memory_Container_Movable $container, $id)
335  {
342  if ($container === $this->_lastModified) {
343  return;
344  }
345 
346  // Remove just updated object from list of candidates to unload
347  if( isset($this->_unloadCandidates[$id])) {
348  unset($this->_unloadCandidates[$id]);
349  }
350 
351  // Reduce used memory mark
352  $this->_memorySize -= $this->_sizes[$id];
353 
354  // Commit changes of previously modified object if necessary
355  $this->_commit();
356 
357  $this->_lastModified = $container;
358  }
359 
363  private function _commit()
364  {
365  if (($container = $this->_lastModified) === null) {
366  return;
367  }
368 
369  $this->_lastModified = null;
370 
371  $id = $container->getId();
372 
373  // Calculate new object size and increase used memory size by this value
374  $this->_memorySize += ($this->_sizes[$id] = strlen($container->getRef()));
375 
376  if ($this->_sizes[$id] > $this->_minSize) {
377  // Move object to "unload candidates list"
378  $this->_unloadCandidates[$id] = $container;
379  }
380 
381  $container->startTrace();
382 
383  $this->_swapCheck();
384  }
385 
391  private function _swapCheck()
392  {
393  if ($this->_memoryLimit < 0 || $this->_memorySize < $this->_memoryLimit) {
394  // Memory limit is not reached
395  // Do nothing
396  return;
397  }
398 
399  // walk through loaded objects in access history order
400  foreach ($this->_unloadCandidates as $id => $container) {
401  $this->_swap($container, $id);
402  unset($this->_unloadCandidates[$id]);
403 
404  if ($this->_memorySize < $this->_memoryLimit) {
405  // We've swapped enough objects
406  return;
407  }
408  }
409 
410  #require_once 'Zend/Memory/Exception.php';
411  throw new Zend_Memory_Exception('Memory manager can\'t get enough space.');
412  }
413 
414 
423  private function _swap(Zend_Memory_Container_Movable $container, $id)
424  {
425  if ($container->isLocked()) {
426  return;
427  }
428 
429  if (!$container->isSwapped()) {
430  $this->_backend->save($container->getRef(), $this->_managerId . $id, $this->_tags);
431  }
432 
433  $this->_memorySize -= $this->_sizes[$id];
434 
435  $container->markAsSwapped();
436  $container->unloadValue();
437  }
438 
446  public function load(Zend_Memory_Container_Movable $container, $id)
447  {
448  $value = $this->_backend->load($this->_managerId . $id, true);
449 
450  // Try to swap other objects if necessary
451  // (do not include specified object into check)
452  $this->_memorySize += strlen($value);
453  $this->_swapCheck();
454 
455  // Add loaded obect to the end of loaded objects list
456  $container->setValue($value);
457 
458  if ($this->_sizes[$id] > $this->_minSize) {
459  // Add object to the end of "unload candidates list"
460  $this->_unloadCandidates[$id] = $container;
461  }
462  }
463 }
$id
Definition: fieldset.phtml:14
load(Zend_Memory_Container_Movable $container, $id)
Definition: Manager.php:446
processUpdate(Zend_Memory_Container_Movable $container, $id)
Definition: Manager.php:334
unlink(Zend_Memory_Container_Movable $container, $id)
Definition: Manager.php:310
setMemoryLimit($newLimit)
Definition: Manager.php:210
$value
Definition: gender.phtml:16
create($value='')
Definition: Manager.php:254
setMinSize($newSize)
Definition: Manager.php:232
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74
createLocked($value='')
Definition: Manager.php:267
__construct($backend=null)
Definition: Manager.php:161