File: examples/demo-api.php

Recommend this page to a friend!
  Classes of Muhammed M  >  PHP Page Cache Output  >  examples/demo-api.php  >  Download  
File: examples/demo-api.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Page Cache Output
Store full page output in cache files
Author: By
Last change: PSR-16 refactoring, Config created
CS fixes
basic refactoring for #1 (introduce PSR-16 cache + implement headers forwarding)
Date: 3 years ago
Size: 2,787 bytes



Class file image Download

 * This file is part of the PageCache package.
 * @author Muhammed Mamedov <>
 * @copyright 2016
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.

 * This demo demonstrates use of global config.php config file in PageCache.
 * It's useful to have settings defined in one file, to avoid repeating yourself

require_once __DIR__ . '/../vendor/autoload.php';


//PageCache configuration in a file
$config_file = __DIR__ . '/config.php';

//pass config file
$cache = new PageCache($config_file);

//enable log, by default disabled in config.php

'Cache key, getCurrentKey(): ' . $cache->getCurrentKey() . '<br/>';

//cache present already?
echo 'isChached(): ';
if (
$cache->isCached()) {
'cache exists';
} else {
'cache does not exist';

//get cache file contents
echo 'Cache file contents: <br/>';
$str = $cache->getPageCache();

//Clear cache for this page
//Uncomment this line below to force cache clear of this page

//disable log, overrides config.php

//Change log file path, overrides config.php

//Set cache expiration for this current page, overrides config.php

//Cache file minimum size, if it's less than this many bytes cache considered invalid.
//This value is important, set to your minimum page size.
//This will ensure that no visitor will see an empty page or some error, in case cache generation fails

//Change cache file location. If needed you can have cache for each URL in a seperate location.

//Set cache strategy. If needed implemet a new strategy class (see src/Strategy/ for code of built-in strategies)
//$cache->setStrategy( new \PageCache\Strategy\MobileStrategy());

//start PageCache

<h1>Example #4 - API. Call to some methods of PageCache.</h1>
<h3>This is a demo PageCache page that is going to be cached. <span style="color: green;">
        Refresh browser to see changes.</span>
<h3 style="color: red">Demo with conf.php configuration file usage, see source code for this file.</h3>
<h3>This is a dynamic PHP <i>date('H:i:s')</i>
    call, note that time doesn't change on refresh: <?php echo date('H:i:s'); ?>.</h3>
<h4>Check examples/cache/ directory to see cached content.
    Erase this file to regenerate cache, or it will automatically be regenerated in 10 minutes, as per conf.php</h4>

For more information send a message to info at phpclasses dot org.