function changeDate($var) { $this->cacheState[] = 'changed-' . $var; // We use array as state. We assume a second call to the changeDate // function must not be considered the same as the first one, and thus a new state is used // if you call the function 3 times with $var='str1'. The state of the third call will be 'changed-var1-changed-var1-changed-var1'. // If the call simply reads from the db and stores the data in the members - you dont need this - // simply use $this->cacheState='changed-'.$var; if (!$this->cacheRestore()) { echo "No cache! Heavy read here!..."; sleep(2); echo "done!\n"; // Lets say this is some heavy compyting here, and the results are written to member1 and 2 $this->member1[] = $var; $this->member2 .= $var; $this->cacheStore(CacheExpire::create()->timeout(60)); } else { echo "Cache was hit!\n"; } }
public $member2 = 'abc'; function changeDate($var) { echo "No cache! Heavy read here!..."; sleep(2); echo "done!\n"; // Lets say this is some heavy compyting here, and the results are written to member1 and 2 $this->member1[] = $var; $this->member2 .= $var; } } $obj = new obj_to_cache2(); $obj->cacheState = 'changed-str1'; if (!$obj->cacheRestore()) { $obj->changeDate('str1'); $obj->cacheStore(CacheExpire::create()->timeout(60)); } else { // Cache was hit! echo "Cache hit!\n"; } var_dump($obj); /** * Outputs (1st execute): * No cache! Heavy read here!...done! object(obj_to_cache2)#1 (4) { ["member1"]=> array(2) { ["a"]=> int(1) [0]=>
public $member2 = 'abc'; function changeDate($var) { echo "No cache! Heavy read here!..."; sleep(2); echo "done!\n"; // Lets say this is some heavy compyting here, and the results are written to member1 and 2 $this->member1[] = $var; $this->member2 .= $var; } } // At a later stage, or another script $obj = new obj_to_cache(); if (!Cacheable::sCacheRestore($obj, 'changed-str1')) { $obj->changeDate('str1'); Cacheable::sCacheStore($obj, 'changed-str1', CacheExpire::create()->timeout(60)); } else { // Cache was hit! echo "Cache hit!\n"; } var_dump($obj); /** * Outputs (1st execute): * No cache! Heavy read here!...done! object(obj_to_cache)#1 (2) { ["member1"]=> array(2) { ["a"]=> int(1) [0]=>
<?php /** * Static use of cacheable - capable of caching any object! */ require 'CacheHelper.php'; CacheHelper::$cacheDir = dirname(__FILE__) . '/cache'; // Configure the cache dir class helper { function fn($var1) { return 'Result from helper->fn @ ' . date('r'); } static function static_fn($var2) { return 'Result from helper::static_fn @ ' . date('r'); } } function fn($var3) { return 'Result from fn @ ' . date('r'); } $cache = new CacheHelper(); echo $cache->cachedCall(array('helper', 'static_fn'), array('value1'), CacheExpire::create()->timeout(60)) . "\n"; echo $cache->cachedCall('helper::static_fn', array('value1'), CacheExpire::create()->timeout(60)) . "\n"; echo $cache->cachedCall('fn', array('value1'), CacheExpire::create()->timeout(60)) . "\n"; $obj = new helper(); echo $cache->cachedCall(array($obj, 'fn'), array('value1'), CacheExpire::create()->timeout(60)) . "\n";
<?php /** * Static use of cacheable - capable of caching any object! */ require 'CacheHelper.php'; CacheHelper::$cacheDir = dirname(__FILE__) . '/cache'; // Configure the cache dir $param1 = 'value2'; $cache = new CacheHelper(); $cache->cachedInclude('CacheHelper_include.php', null, CacheExpire::create()->timeout(60));