예제 #1
0
<?php

/**
 * Cache objects that extend Cacheable. Use w/out altering the data modification method
 * All the caching is done OUTSIDE of the method that reads/computes the cached data
 */
require 'Cacheable.php';
Cacheable::$cacheDir = dirname(__FILE__) . '/cache';
// Configure the cache dir
class obj_to_cache2 extends Cacheable
{
    public $member1 = array('a' => 1);
    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";