Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes
Cache Class Reference

Public Member Functions

 load ($idx, $default=null)
 
 save ($object, $idx=null, $tags=null)
 
 reference ($refName, $idx)
 
 delete ($idx)
 
 deleteByClass ($class)
 
 deleteByTags ($tags)
 
 has ($idx)
 
 find ($object)
 
 findByIds ($ids)
 
 findByHash ($hash)
 
 findByTags ($tags)
 
 findByClass ($class)
 
 debug ($idx, $object=null)
 
 debugByIds ($ids)
 
 getAllObjects ()
 
 getAllTags ()
 
 getAllTagsByObject ()
 
 getAllReferences ()
 

Static Public Member Functions

static singleton ()
 

Protected Attributes

 $_idx = 0
 
 $_objects = []
 
 $_hashes = []
 
 $_objectHashes = []
 
 $_tags = []
 
 $_objectTags = []
 
 $_references = []
 
 $_objectReferences = []
 
 $_debug = []
 

Static Protected Attributes

static $_instance
 

Detailed Description

Object Cache

Stores objects for reuse, cleanup and to avoid circular references

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m

Definition at line 15 of file Cache.php.

Member Function Documentation

◆ debug()

debug (   $idx,
  $object = null 
)

Debug

Parameters
string$idx
object | null$object
Returns
void @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 395 of file Cache.php.

396  {
397  $bt = debug_backtrace();
398  $debug = [];
399  foreach ($bt as $i => $step) {
400  $debug[$i] = [
401  'file' => isset($step['file']) ? $step['file'] : null,
402  'line' => isset($step['line']) ? $step['line'] : null,
403  'function' => isset($step['function']) ? $step['function'] : null,
404  ];
405  }
406  $this->_debug[$idx] = $debug;
407  }
$debug
Definition: router.php:22
$i
Definition: gallery.phtml:31

◆ debugByIds()

debugByIds (   $ids)

Return debug information by ids

Parameters
array | string$ids
Returns
array

Definition at line 415 of file Cache.php.

416  {
417  if (is_string($ids)) {
418  $ids = [$ids];
419  }
420  $debug = [];
421  foreach ($ids as $idx) {
422  $debug[$idx] = $this->_debug[$idx];
423  }
424  return $debug;
425  }
$debug
Definition: router.php:22

◆ delete()

delete (   $idx)

Delete an object from registry

Parameters
string | object$idx
Returns
boolean

Definition at line 217 of file Cache.php.

218  {
219  //\Magento\Framework\Profiler::start("OBJECT_DELETE");
220  if (is_object($idx)) {
221  $idx = $this->find($idx);
222  if (false === $idx) {
223  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
224  return false;
225  }
226  unset($this->_objects[$idx]);
227  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
228  return false;
229  } elseif (!isset($this->_objects[$idx])) {
230  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
231  return false;
232  }
233 
234  unset($this->_objects[$idx]);
235 
236  unset($this->_hashes[$this->_objectHashes[$idx]], $this->_objectHashes[$idx]);
237 
238  if (isset($this->_objectTags[$idx])) {
239  foreach ($this->_objectTags[$idx] as $t => $dummy) {
240  unset($this->_tags[$t][$idx]);
241  }
242  unset($this->_objectTags[$idx]);
243  }
244 
245  if (isset($this->_objectReferences[$idx])) {
246  foreach ($this->_references as $r => $dummy) {
247  unset($this->_references[$r]);
248  }
249  unset($this->_objectReferences[$idx]);
250  }
251  //\Magento\Framework\Profiler::stop("OBJECT_DELETE");
252 
253  return true;
254  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ deleteByClass()

deleteByClass (   $class)

Cleanup by class name for objects of subclasses too

Parameters
string$class
Returns
void

Definition at line 262 of file Cache.php.

263  {
264  foreach ($this->_objects as $idx => $object) {
265  if ($object instanceof $class) {
266  $this->delete($idx);
267  }
268  }
269  }
$_option $_optionId $class
Definition: date.phtml:13

◆ deleteByTags()

deleteByTags (   $tags)

Cleanup objects by tags

Parameters
array | string$tags
Returns
true @SuppressWarnings(PHPMD.UnusedLocalVariable)

Definition at line 278 of file Cache.php.

279  {
280  if (is_string($tags)) {
281  $tags = [$tags];
282  }
283  foreach ($tags as $t) {
284  foreach ($this->_tags[$t] as $idx => $dummy) {
285  $this->delete($idx);
286  }
287  }
288  return true;
289  }

◆ find()

find (   $object)

Find an object id

Parameters
object$object
Returns
string|boolean

Definition at line 308 of file Cache.php.

309  {
310  foreach ($this->_objects as $idx => $obj) {
311  if ($object === $obj) {
312  return $idx;
313  }
314  }
315  return false;
316  }

◆ findByClass()

findByClass (   $class)

Find by class name for objects of subclasses too

Parameters
string$class
Returns
array

Definition at line 376 of file Cache.php.

377  {
378  $objects = [];
379  foreach ($this->_objects as $idx => $object) {
380  if ($object instanceof $class) {
381  $objects[$idx] = $object;
382  }
383  }
384  return $objects;
385  }
$_option $_optionId $class
Definition: date.phtml:13

◆ findByHash()

findByHash (   $hash)

Find object by hash

Parameters
string$hash
Returns
object

Definition at line 341 of file Cache.php.

342  {
343  return isset($this->_hashes[$hash]) ? $this->_objects[$this->_hashes[$hash]] : null;
344  }

◆ findByIds()

findByIds (   $ids)

Find objects by ids

Parameters
string[]$ids
Returns
array

Definition at line 324 of file Cache.php.

325  {
326  $objects = [];
327  foreach ($this->_objects as $idx => $obj) {
328  if (in_array($idx, $ids)) {
329  $objects[$idx] = $obj;
330  }
331  }
332  return $objects;
333  }

◆ findByTags()

findByTags (   $tags)

Find objects by tags

Parameters
array | string$tags
Returns
array @SuppressWarnings(PHPMD.UnusedLocalVariable)

Definition at line 353 of file Cache.php.

354  {
355  if (is_string($tags)) {
356  $tags = [$tags];
357  }
358  $objects = [];
359  foreach ($tags as $t) {
360  foreach ($this->_tags[$t] as $idx => $dummy) {
361  if (isset($objects[$idx])) {
362  continue;
363  }
364  $objects[$idx] = $this->load($idx);
365  }
366  }
367  return $objects;
368  }
load($idx, $default=null)
Definition: Cache.php:107

◆ getAllObjects()

getAllObjects ( )

Get all objects

Returns
array

Definition at line 432 of file Cache.php.

433  {
434  return $this->_objects;
435  }

◆ getAllReferences()

getAllReferences ( )

Get all references

Returns
array

Definition at line 462 of file Cache.php.

463  {
464  return $this->_references;
465  }

◆ getAllTags()

getAllTags ( )

Get all tags

Returns
array

Definition at line 442 of file Cache.php.

443  {
444  return $this->_tags;
445  }

◆ getAllTagsByObject()

getAllTagsByObject ( )

Get all tags by object

Returns
array

Definition at line 452 of file Cache.php.

453  {
454  return $this->_objectTags;
455  }

◆ has()

has (   $idx)

Check whether object id exists in registry

Parameters
string$idx
Returns
boolean

Definition at line 297 of file Cache.php.

298  {
299  return isset($this->_objects[$idx]) || isset($this->_references[$idx]);
300  }

◆ load()

load (   $idx,
  $default = null 
)

Load an object from registry

Parameters
string | object$idx
object$default
Returns
object

Definition at line 107 of file Cache.php.

108  {
109  if (isset($this->_references[$idx])) {
110  $idx = $this->_references[$idx];
111  }
112  if (isset($this->_objects[$idx])) {
113  return $this->_objects[$idx];
114  }
115  return $default;
116  }

◆ reference()

reference (   $refName,
  $idx 
)

Add a reference to an object

Parameters
string | array$refName
string$idx
Returns
bool|void
Exceptions

Definition at line 188 of file Cache.php.

189  {
190  if (is_array($refName)) {
191  foreach ($refName as $ref) {
192  $this->reference($ref, $idx);
193  }
194  return;
195  }
196 
197  if (isset($this->_references[$refName])) {
198  throw new \Magento\Framework\Exception\LocalizedException(
199  new \Magento\Framework\Phrase(
200  'The reference already exists: %1. New index: %2, old index: %3',
201  [$refName, $idx, $this->_references[$refName]]
202  )
203  );
204  }
205  $this->_references[$refName] = $idx;
206  $this->_objectReferences[$idx][$refName] = true;
207 
208  return true;
209  }

◆ save()

save (   $object,
  $idx = null,
  $tags = null 
)

Save an object entry

Parameters
object$object
string$idx
array | string$tags
Returns
string
Exceptions

Definition at line 129 of file Cache.php.

130  {
131  //\Magento\Framework\Profiler::start('OBJECT_SAVE');
132  if (!is_object($object)) {
133  return false;
134  }
135 
136  $hash = spl_object_hash($object);
137  if ($idx !== null && strpos($idx, '{') !== false) {
138  $idx = str_replace('{hash}', $hash, $idx);
139  }
140  if (isset($this->_hashes[$hash])) {
141  //throw new \Exception('test');
142  if ($idx !== null) {
143  $this->_references[$idx] = $this->_hashes[$hash];
144  }
145  return $this->_hashes[$hash];
146  }
147 
148  if ($idx === null) {
149  $idx = '#' . ++$this->_idx;
150  }
151 
152  if (isset($this->_objects[$idx])) {
153  throw new \Magento\Framework\Exception\LocalizedException(
154  new \Magento\Framework\Phrase(
155  'Object already exists in registry (%1). Old object class: %2, new object class: %3',
156  [$idx, get_class($this->_objects[$idx]), get_class($object)]
157  )
158  );
159  }
160 
161  $this->_objects[$idx] = $object;
162 
163  $this->_hashes[$hash] = $idx;
164  $this->_objectHashes[$idx] = $hash;
165 
166  if (is_string($tags)) {
167  $this->_tags[$tags][$idx] = true;
168  $this->_objectTags[$idx][$tags] = true;
169  } elseif (is_array($tags)) {
170  foreach ($tags as $t) {
171  $this->_tags[$t][$idx] = true;
172  $this->_objectTags[$idx][$t] = true;
173  }
174  }
175  //\Magento\Framework\Profiler::stop('OBJECT_SAVE');
176 
177  return $idx;
178  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ singleton()

static singleton ( )
static

Singleton factory

Returns
\Magento\Framework\DataObject\Cache

Definition at line 92 of file Cache.php.

93  {
94  if (!self::$_instance) {
95  self::$_instance = new self();
96  }
97  return self::$_instance;
98  }

Field Documentation

◆ $_debug

$_debug = []
protected

Definition at line 85 of file Cache.php.

◆ $_hashes

$_hashes = []
protected

Definition at line 43 of file Cache.php.

◆ $_idx

$_idx = 0
protected

Definition at line 29 of file Cache.php.

◆ $_instance

$_instance
staticprotected

Definition at line 22 of file Cache.php.

◆ $_objectHashes

$_objectHashes = []
protected

Definition at line 50 of file Cache.php.

◆ $_objectReferences

$_objectReferences = []
protected

Definition at line 78 of file Cache.php.

◆ $_objects

$_objects = []
protected

Definition at line 36 of file Cache.php.

◆ $_objectTags

$_objectTags = []
protected

Definition at line 64 of file Cache.php.

◆ $_references

$_references = []
protected

Definition at line 71 of file Cache.php.

◆ $_tags

$_tags = []
protected

Definition at line 57 of file Cache.php.


The documentation for this class was generated from the following file: