www.gusucode.com > KPPW众包威客PHP开源建站系统 v3.0源码程序 > KPPW/vendor/gregwar/cache/Gregwar/Cache/demo/max-age.php

    <?php

include('../autoload.php');

$cache = new Gregwar\Cache\Cache;

$data = $cache->getOrCreate('uppercase.txt', array('max-age' => 2), function() {
    echo "First call: generating file...\n";
    return strtoupper(file_get_contents('original.txt'));
});

$data = $cache->getOrCreate('uppercase.txt', array('max-age' => 2), function() {
    echo "Second call: generating file, this should not happen!...\n";
    return strtoupper(file_get_contents('original.txt'));
});

echo "Waiting 4s...\n";
sleep(4);

$data = $cache->getOrCreate('uppercase.txt', array('max-age' => 2), function() {
    echo "Third call: generating cache file, because it expired...\n";
    return strtoupper(file_get_contents('original.txt'));
});