Beispiel #1
0
 function cachedInclude($script, $env = array(), $expire = null)
 {
     $this->cacheMembers = array('html');
     $this->cacheState = 'include-' . str_replace('\\', '-', str_replace('/', '-', $script));
     $this->cacheState .= '-' . abs(crc32(serialize($env)));
     if (!$this->cacheRestore()) {
         extract($env);
         ob_start();
         include $script;
         $this->html = ob_get_flush();
         if (!$expire) {
             $expire = new CacheExpire();
         }
         $expire->filemtimeChanged($script);
         // The cache is invalid if the included file has changed
         $this->cacheStore($expire);
     } else {
         echo $this->html;
     }
 }
Beispiel #2
0
 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";
     }
 }
Beispiel #3
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;
    }
}
$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]=>
Beispiel #4
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]=>
Beispiel #5
0
 private function loadDefinitions($classes)
 {
     if ($this->definitions_loaded) {
         return;
     }
     $this->cacheState = $classes;
     array_unshift($this->cacheState, 'defs');
     if (!$this->cacheRestore()) {
         $exp = new CacheExpire();
         foreach ($classes as $class) {
             $doc = new php_doc($class);
             $refl = new ReflectionClass($class);
             $vers = $doc->getTags('version', php_doc::TYPE_GENERIC);
             if (!$vers[0]) {
                 $vers = $doc->getTags('ver', php_doc::TYPE_GENERIC);
             }
             $ver = @$vers[0]['name'];
             if (!isset($ver)) {
                 $ver = '0';
             }
             $this->versions[$class] = $ver;
             $historys = $doc->getTags('history', php_doc::TYPE_KEYVALUE);
             if ($historys[0] && $historys[0]['key']) {
                 $file = $historys[0]['value'];
                 if ($file && $file[0] != '/' && $file[0] != '\\') {
                     $file = dirname($refl->getFileName()) . '/' . $file;
                 }
                 $this->histories[$class] = array($historys[0]['key'], $file);
             } else {
                 continue;
             }
             $exp->filemtimeChanged($refl->getFileName());
         }
         $this->cacheMembers = array('versions', 'histories');
         $this->cacheStore($exp);
     }
 }
Beispiel #6
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";
Beispiel #7
0
<?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));