Actions

Talk

JFactory/getCache

From Joomla! Documentation

Example?

The example of the cache is a little bit weird.

// Define a class to contain the computationally-expensive method.
class expensiveClass {
 
  function expensiveMethod ( $signature ) {
 
    static $instances;
 
    if (!isset( $instances )) $instances = array();
 
    if (empty( $instances[$signature] )) {
 
      // Something that is resource-intensive to produce, here simulated
      // by calculating the MD5 hash of the signature.
 
      $instances[$signature] = md5( $signature );
      echo 'Cache miss';
 
    } else {
 
      echo 'Cache hit ';
 
    }
 
    return $instances[$signature];
  }
}
 
echo ' : ' . expensiveClass::expensiveMethod('abc') . "\n";
echo ' : ' . expensiveClass::expensiveMethod('efg') . "\n";
echo ' : ' . expensiveClass::expensiveMethod('abc') . "\n";
echo ' : ' . expensiveClass::expensiveMethod('hij') . "\n";
echo ' : ' . expensiveClass::expensiveMethod('klm') . "\n";
echo ' : ' . expensiveClass::expensiveMethod('hij') . "\n";

Would also output:

Cache miss : 900150983cd24fb0d6963f7d28e17f72
Cache miss : 7d09898e18511cf7c0c1815d07728d23
Cache hit  : 900150983cd24fb0d6963f7d28e17f72
Cache miss : 857c4402ad934005eae4638a93812bf7
Cache miss : 3491f0dc1059a35bb1681b3bd67cb0d5
Cache hit  : 857c4402ad934005eae4638a93812bf7

So I think, that this is a really bad example for the cache function. Perhaps you should use this example?

--Bembelimen 13:38, 16 June 2009 (UTC)