Talk

JFactory/getCache

From Joomla! Documentation

Revision as of 08:39, 16 June 2009 by Bembelimen (talk | contribs) (New page: == Example? == The example of the cache is a little bit weird. <source lang="php"> // Define a class to contain the computationally-expensive method. class expensiveClass { function e...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Example?[edit]

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)