// =================================================
// === Classical use of the "Core" of Zend_Cache ===
// =================================================

<?php

require_once 'Zend/Cache.php';

$frontendOptions = array(
    'lifetime' => 7200 // cache lifetime of 2 hours
);

$backendOptions = array(
    'cache_dir' => '/tmp/' // Directory where to put the cache files (make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

$id = 'foo'; // cache id of "what we want to cache"

if (!($cache->test($id))) {
    // cache missed
    
    require_once ... // for perfs reasons, all "require_once" have to be loaded ONLY if the cache is missed
    require_once ...
    
    // we build "what we want to cache"
    // for example
    $data = '';
    for ($i=0;$i<10000;$i++) {
        $data = $data . $i;
    } 
    
    // We save the result into the cache
    $cache->save($data);

} else {
    // cache hit
    
    $data = $cache->load($id);

}

// do something with $data :)
// [...]

?>


// =================================================
// === "Compact" use of the "Core" of Zend_Cache ===
// =================================================

// This is ok if you store only strings into the cache
// (because with "automatic_serialization" option, it's possible to store
//  some booleans into cache)

<?php

// [...] // require and configuration

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
$id = 'foo'; // cache id of "what we want to cache"

if (!($data = $cache->load($id))) {
    // cache missed
    
    $data = '';
    for ($i=0;$i<10000;$i++) {
        $data = $data . $i;
    }
    
    $cache->save($data);
    
} 

echo($data);

?>


// =================================================
// === "Compact" use of the "Core" of Zend_Cache ===
// === (example with two blocks)                 ===
// =================================================

// This is ok if you store only strings into the cache
// (because with "automatic_serialization" option, it's possible to store
//  some booleans into cache)

<?php

// [...] // require and configuration

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
$id1 = 'foo'; // cache id of block1
$id2 = 'bar'; // cache id of block2

// BLOCK1
if (!($data = $cache->load($id1))) {
    // cache missed
    
    $data = '';
    for ($i=0;$i<10000;$i++) {
        $data = $data . $i;
    }
    
    $cache->save($data);
    
} 
echo($data);

// NEVER CACHED BLOCK
echo('NEVER CACHED !');

// BLOCK2
if (!($data = $cache->load($id2))) {
    // cache missed
    
    $data = '';
    for ($i=0;$i<10000;$i++) {
        $data = $data . '!';
    }
    
    $cache->save($data);
    
} 
echo($data);

?>


// =========================================================
// === "Compact" use of the "Core" of Zend_Cache         ===
// === (example with two blocks and different lifetimes) ===
// =========================================================

<?php

// [...] // require and configuration

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
$id1 = 'foo'; // cache id of block1
$id2 = 'bar'; // cache id of block2

// BLOCK1
if (!($data = $cache->load($id1))) {
    // cache missed
    
    $data = '';
    for ($i=0;$i<10000;$i++) {
        $data = $data . $i;
    }
    
    $cache->save($data);
    
} 
echo($data);

// NEVER CACHED BLOCK
echo('NEVER CACHED !');

// BLOCK2
if (!($data = $cache->load($id2))) {
    // cache missed
    
    $data = '';
    for ($i=0;$i<10000;$i++) {
        $data = $data . '!';
    }
    
    $cache->save($data, null, array(), 3600);
    // => this cache will have a specific lifetime of 3600 seconds
    
} 
echo($data);

?>


// ============================================================
// === Classical use of the "Output" frontend of Zend_Cache ===
// ============================================================

<?php

require_once 'Zend/Cache.php';

$frontendOptions = array(
    'lifetime' => 7200 // cache lifetime of 2 hours
);

$backendOptions = array(
    'cache_dir' => '/tmp/' // Directory where to put the cache files (make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);

$id = 'foo'; // cache id of "what we want to cache"

if (!($cache->start($id))) {
    // cache is not hit !
    
    // Output you want to cache
    for ($i=0;$i<10000;$i++) {
        echo($i);
    }

    // store "captured" output into cache
    $cache->end();
}

?>


// ==============================================================
// === Classical use of the "Function" frontend of Zend_Cache ===
// ==============================================================

<?php

require_once 'Zend/Cache.php';

$frontendOptions = array(
    'lifetime' => 7200 // cache lifetime of 2 hours
);

$backendOptions = array(
    'cache_dir' => '/tmp/' // Directory where to put the cache files (make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('Function', 'File', $frontendOptions, $backendOptions);

function function_to_cache($arg1, $arg2) {
    echo("called function_to_cache($arg1, $arg2)");
    return $arg1 + $arg2;
}

// First call, the function will be called
$res1 = $cache->call('function_to_cache', array(1, 3));
echo($res1);

// Second call, output and result will be get from cache
$res2 = $cache->call('function_to_cache', array(1, 3));
echo($res2);

// Third call, the function will be called (because argument values are different)
$res3 = $cache->call('function_to_cache', array(2, 5));
echo($res3);

?>


// ===========================================================
// === Classical use of the "Class" frontend of Zend_Cache ===
// === (mode : class)                                      ===
// ===========================================================

<?php

// Class to cache
class test {

    public static function foobar($param1, $param2) {
        echo "foobar_output($param1, $param2)";
        return "foobar_return($param1, $param2)";   
    }

}

require_once 'Zend/Cache.php';

$frontendOptions = array(
    'lifetime' => 7200 // cache lifetime of 2 hours,
    'cached_entity' => 'test'
);

$backendOptions = array(
    'cache_dir' => '/tmp/' // Directory where to put the cache files (make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('Class', 'File', $frontendOptions, $backendOptions);

// First call, the static method will be called
$res1 = $cache->foobar(1, 3);
echo($res1);

// Second call, output and result will be get from cache
$res2 = $cache->foobar(1, 3);
echo($res2);

// Third call, the method will be called (because argument values are different)
$res3 = $cache->foobar(2, 5)
echo($res3);

?>


// ===========================================================
// === Classical use of the "Class" frontend of Zend_Cache ===
// === (mode : object)                                     ===
// ===========================================================

<?php

// Class to cache
class test {

    private $_string = 'hello !';
      
    public function foobar2($param1, $param2) {
        echo($this->_string);
        echo "foobar2_output($param1, $param2)";
        return "foobar2_return($param1, $param2)";   
    }

}

require_once 'Zend/Cache.php';

$frontendOptions = array(
    'lifetime' => 7200 // cache lifetime of 2 hours,
    'cached_entity' => new test()
);

$backendOptions = array(
    'cache_dir' => '/tmp/' // Directory where to put the cache files (make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('Class', 'File', $frontendOptions, $backendOptions);

// First call, the method will be called
$res1 = $cache->foobar(1, 3);
echo($res1);

// Second call, output and result will be get from cache
$res2 = $cache->foobar(1, 3);
echo($res2);

// Third call, the method will be called (because argument values are different)
$res3 = $cache->foobar(2, 5)
echo($res3);

?>


// ==========================================================
// === Classical use of the "File" frontend of Zend_Cache ===
// ==========================================================

<?php

// The file frontend is used to get a cache driven by a master file
// for example, you have a config file declared as master file
// => all your cache records will be invalidated if the config file is touched
// (very usefull to avoid the parsing of a XML file at each time for example)

require_once 'Zend/Cache.php';

$frontendOptions = array(
    'lifetime' => null // no lifetime,
    'master_file' => '/path/to/your/master.file' // your master file here
);

$backendOptions = array(
    'cache_dir' => '/tmp/' // Directory where to put the cache files (make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('File', 'File', $frontendOptions, $backendOptions);

// [...] identical to the "Core" use

?>