Varien_Object_Cache Class Reference

List of all members.

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 ()
 getAllReferencesByObject ()

Static Public Member Functions

static singleton ()

Protected Attributes

 $_idx = 0
 $_objects = array()
 $_hashes = array()
 $_objectHashes = array()
 $_tags = array()
 $_objectTags = array()
 $_references = array()
 $_objectReferences = array()
 $_debug = array()

Static Protected Attributes

static $_instance


Detailed Description

Object Cache

Stores objects for reuse, cleanup and to avoid circular references

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 35 of file Cache.php.


Member Function Documentation

debug ( idx,
object = null 
)

Definition at line 376 of file Cache.php.

00377     {
00378         $bt = debug_backtrace();
00379         $debug = array();
00380         foreach ($bt as $i=>$step) {
00381             $debug[$i] = array(
00382                 'file'     => isset($step['file']) ? $step['file'] : null,
00383                 'line'     => isset($step['line']) ? $step['line'] : null,
00384                 'function' => isset($step['function']) ? $step['function'] : null,
00385             );
00386         }
00387         $this->_debug[$idx] = $debug;
00388     }

debugByIds ( ids  ) 

Return debug information by ids

Parameters:
array|integer $ids
Returns:
array

Definition at line 396 of file Cache.php.

00397     {
00398         if (is_string($ids)) {
00399             $ids = array($ids);
00400         }
00401         $debug = array();
00402         foreach ($ids as $idx) {
00403             $debug[$idx] = $this->_debug[$idx];
00404         }
00405         return $debug;
00406     }

delete ( idx  ) 

Delete an object from registry

Parameters:
string|object $idx
Returns:
boolean

Definition at line 223 of file Cache.php.

00224     {
00225 //Varien_Profiler::start("OBJECT_DELETE");
00226         if (is_object($idx)) {
00227             $idx = $this->find($idx);
00228             if (false===$idx) {
00229 //Varien_Profiler::stop("OBJECT_DELETE");
00230                 return false;
00231             }
00232             unset($this->_objects[$idx]);
00233 //Varien_Profiler::stop("OBJECT_DELETE");
00234             return false;
00235         } elseif (!isset($this->_objects[$idx])) {
00236 //Varien_Profiler::stop("OBJECT_DELETE");
00237             return false;
00238         }
00239 
00240         unset($this->_objects[$idx]);
00241 
00242         unset($this->_hashes[$this->_objectHashes[$idx]], $this->_objectHashes[$idx]);
00243 
00244         if (isset($this->_objectTags[$idx])) {
00245             foreach ($this->_objectTags[$idx] as $t=>$dummy) {
00246                 unset($this->_tags[$t][$idx]);
00247             }
00248             unset($this->_objectTags[$idx]);
00249         }
00250 
00251         if (isset($this->_objectReferences[$idx])) {
00252             foreach ($references as $r=>$dummy) {
00253                 unset($this->_references[$r]);
00254             }
00255             unset($this->_objectReferences[$idx]);
00256         }
00257 //Varien_Profiler::stop("OBJECT_DELETE");
00258 
00259         return true;
00260     }

deleteByClass ( class  ) 

Cleanup by class name for objects of subclasses too

Parameters:
string $class

Definition at line 267 of file Cache.php.

00268     {
00269         foreach ($this->_objects as $idx=>$object) {
00270             if ($object instanceof $class) {
00271                 $this->delete($idx);
00272             }
00273         }
00274     }

deleteByTags ( tags  ) 

Cleanup objects by tags

Parameters:
array|string $tags

Definition at line 281 of file Cache.php.

00282     {
00283         if (is_string($tags)) {
00284             $tags = array($tags);
00285         }
00286         foreach ($tags as $t) {
00287             foreach ($this->_tags[$t] as $idx=>$dummy) {
00288                 $this->delete($idx);
00289             }
00290         }
00291         return true;
00292     }

find ( object  ) 

Find an object id

Parameters:
object $object
Returns:
string|boolean

Definition at line 311 of file Cache.php.

00312     {
00313         foreach ($this->_objects as $idx=>$obj) {
00314             if ($object===$obj) {
00315                 return $idx;
00316             }
00317         }
00318         return false;
00319     }

findByClass ( class  ) 

Find by class name for objects of subclasses too

Parameters:
string $class

Definition at line 365 of file Cache.php.

00366     {
00367         $objects = array();
00368         foreach ($this->_objects as $idx=>$object) {
00369             if ($object instanceof $class) {
00370                 $objects[$idx] = $object;
00371             }
00372         }
00373         return $objects;
00374     }

findByHash ( hash  ) 

Definition at line 332 of file Cache.php.

00333     {
00334         return isset($this->_hashes[$hash]) ? $this->_objects[$this->_hashes[$hash]] : null;
00335     }

findByIds ( ids  ) 

Definition at line 321 of file Cache.php.

00322     {
00323         $objects = array();
00324         foreach ($this->_objects as $idx=>$obj) {
00325             if (in_array($idx, $ids)) {
00326                 $objects[$idx] = $obj;
00327             }
00328         }
00329         return $objects;
00330     }

findByTags ( tags  ) 

Find objects by tags

Parameters:
array|string $tags
Returns:
array

Definition at line 343 of file Cache.php.

00344     {
00345         if (is_string($tags)) {
00346             $tags = array($tags);
00347         }
00348         $objects = array();
00349         foreach ($tags as $t) {
00350             foreach ($this->_tags[$t] as $idx=>$dummy) {
00351                 if (isset($objects[$idx])) {
00352                     continue;
00353                 }
00354                 $objects[$ids] = $this->load($idx);
00355             }
00356         }
00357         return $objects;
00358     }

getAllObjects (  ) 

Get all objects

Returns:
array

Definition at line 413 of file Cache.php.

00414     {
00415         return $this->_objects;
00416     }

getAllReferences (  ) 

Get all references

Returns:
array

Definition at line 443 of file Cache.php.

00444     {
00445         return $this->_references;
00446     }

getAllReferencesByObject (  ) 

Get all references by object

Returns:
array

Definition at line 453 of file Cache.php.

00454     {
00455         return $this->_referencesByObject;
00456     }

getAllTags (  ) 

Get all tags

Returns:
array

Definition at line 423 of file Cache.php.

00424     {
00425         return $this->_tags;
00426     }

getAllTagsByObject (  ) 

Get all tags by object

Returns:
array

Definition at line 433 of file Cache.php.

00434     {
00435         return $this->_objectTags;
00436     }

has ( idx  ) 

Check whether object id exists in registry

Parameters:
string $idx
Returns:
boolean

Definition at line 300 of file Cache.php.

00301     {
00302         return isset($this->_objects[$idx]) || isset($this->_references[$idx]);
00303     }

load ( idx,
default = null 
)

Load an object from registry

Parameters:
string|object $idx
object $default
Returns:
object

Definition at line 127 of file Cache.php.

00128     {
00129         if (isset($this->_references[$idx])) {
00130             $idx = $this->_references[$idx];
00131         }
00132         if (isset($this->_objects[$idx])) {
00133             return $this->_objects[$idx];
00134         }
00135         return $default;
00136     }

reference ( refName,
idx 
)

Add a reference to an object

Parameters:
string|array $refName
string $idx
Returns:
boolean

Definition at line 199 of file Cache.php.

00200     {
00201         if (is_array($refName)) {
00202             foreach ($refName as $ref) {
00203                 $this->reference($ref, $idx);
00204             }
00205             return;
00206         }
00207 
00208         if (isset($this->_references[$refName])) {
00209             throw new Varien_Exception('The reference already exists: '.$refName.'. New index: '.$idx.', old index: '.$this->_references[$refName]);
00210         }
00211         $this->_references[$refName] = $idx;
00212         $this->_objectReferences[$idx][$refName] = true;
00213 
00214         return true;
00215     }

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

Save an object entry

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

Definition at line 146 of file Cache.php.

00147     {
00148 //Varien_Profiler::start('OBJECT_SAVE');
00149         if (!is_object($object)) {
00150             return false;
00151         }
00152 
00153         $hash = spl_object_hash($object);
00154         if (!is_null($idx) && strpos($idx, '{')) {
00155             $idx = str_replace('{hash}', $hash, $idx);
00156         }
00157         if (isset($this->_hashes[$hash])) {
00158             //throw new Exception('test');
00159             if (!is_null($idx)) {
00160                 $this->_references[$idx] = $this->_hashes[$hash];
00161             }
00162             return $this->_hashes[$hash];
00163         }
00164 
00165         if (is_null($idx)) {
00166             $idx = '#'.(++$this->_idx);
00167         }
00168 
00169         if (isset($this->_objects[$idx])) {
00170             throw new Varien_Exception('Object already exists in registry ('.$idx.'). Old object class: '.get_class($this->_objects[$idx]).', new object class: '.get_class($object));
00171         }
00172 
00173         $this->_objects[$idx] = $object;
00174 
00175         $this->_hashes[$hash] = $idx;
00176         $this->_objectHashes[$idx] = $hash;
00177 
00178         if (is_string($tags)) {
00179             $this->_tags[$tags][$idx] = true;
00180             $this->_objectTags[$idx][$tags] = true;
00181         } elseif (is_array($tags)) {
00182             foreach ($tags as $t) {
00183                 $this->_tags[$t][$idx] = true;
00184                 $this->_objectTags[$idx][$t] = true;
00185             }
00186         }
00187 //Varien_Profiler::stop('OBJECT_SAVE');
00188 
00189         return $idx;
00190     }

static singleton (  )  [static]

Singleton factory

Returns:
Varien_Object_Cache

Definition at line 112 of file Cache.php.

00113     {
00114         if (!self::$_instance) {
00115             self::$_instance = new self();
00116         }
00117         return self::$_instance;
00118     }


Member Data Documentation

$_debug = array() [protected]

Definition at line 105 of file Cache.php.

$_hashes = array() [protected]

Definition at line 63 of file Cache.php.

$_idx = 0 [protected]

Definition at line 49 of file Cache.php.

$_instance [static, protected]

Definition at line 42 of file Cache.php.

$_objectHashes = array() [protected]

Definition at line 70 of file Cache.php.

$_objectReferences = array() [protected]

Definition at line 98 of file Cache.php.

$_objects = array() [protected]

Definition at line 56 of file Cache.php.

$_objectTags = array() [protected]

Definition at line 84 of file Cache.php.

$_references = array() [protected]

Definition at line 91 of file Cache.php.

$_tags = array() [protected]

Definition at line 77 of file Cache.php.


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

Generated on Sat Jul 4 17:25:04 2009 for Magento by  doxygen 1.5.8