PHP code example of aalfiann / buffer-cache

1. Go to this page and download the library: Download aalfiann/buffer-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aalfiann / buffer-cache example snippets


use aalfiann\BufferCache\FilesystemBufferCache;

function modify($buffer) {
    // Test inject javascript
    $javascript = '<script>console.log("Cache was generated at '.date('Y-m-d H:i:s').'")</script>';
    $buffer = explode('</body>',$buffer);
    $buffer = implode($javascript.'</body>',$buffer);
    return $buffer;
}

$cache = new FilesystemBufferCache([
    // Set ttl cache
    'ttl' => 120
]);

// Start cache
$cache->start();

// Start buffer
//$cache->startBuffer();        // without callback
$cache->startBuffer('modify');  // with callback

// Example to render page
echo '<html>
    <head>
        <title>Test Page</title>
    </head>
    <body>Just test to cache the response page</body>
</html>';

// for condition if page failed to render
//$cache->cancelBuffer();

// End cache
//$cache->end();        // without callback
$cache->end('modify');  // with callback

    use aalfiann\BufferCache\FilesystemBufferCache;
    $cache = new FilesystemBufferCache([
        // options here
    ]);
    

    use aalfiann\BufferCache\SQLiteBufferCache;
    $cache = new SQLiteBufferCache([
        // options here
    ]);
    

    use aalfiann\BufferCache\PredisBufferCache;
    $cache = new PredisBufferCache([
        // options here
    ]);
    

[
    'namespace' => 'page',              // Namespace for cache
    'ttl' => 18000,                     // Time to live cache
    'http_cache' => false,              // Use http cache
    'http_maxage' => 3600,              // Maxage of http cache
    'cache_empty_content' => false,     // Cache empty content
    'cache_query_param' => false,       // Allow cache for url with query parameter
    'ext' => [                          // Allow cache for url with extension 
        '.htm','.html','.xhtml','.asp','.aspx','.css',
        '.php','.js','.jsp','.cfm','.md','.xml','.rss'
    ],
    'filesystem' => [                   // filesystem parameters or options
        'path' => 'cache/page'
    ],
    'sqlite3' => [                      // sqlite3 parameters or options
        'table' => 'cache',
        'path' => 'cache/page/page_cache.sqlite3'
    ],
    'predis' => [                       // predis parameters or options.
        'scheme' => 'tcp',
        'host'   => '127.0.0.1',
        'port'   => 6379
    ]
]

var $ext = [
    '.htm','.html','.xhtml','.asp','.aspx','.css',
    '.php','.js','.jsp','.cfm','.md','.xml','.rss'
];

$cache->addExtension('.py');
$cache->addExtension('.txt');

    $cache = new SQLiteBufferCache([
        'ext' => ['.js', '.css']
    ]);
    

    $cache->ext = ['.js', '.css'];
    

    $cache = new SQLiteBufferCache([
        'http_cache' => true,
        'http_maxage' => 3600
    ]);
    

    $cache->useHttpCache(3600);
    

    $cache->http_cache = true;
    $cache->http_maxage = 3600;