PHP code example of nickjbedford / block-cache

1. Go to this page and download the library: Download nickjbedford/block-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/ */

    

nickjbedford / block-cache example snippets


$cacher = BlockCacher\BlockCacher::createDefault(__DIR__ . '/cache');
$sameCacher = blockCacher();

$otherCacher = new BlockCacher\BlockCacher(__DIR__ . '/other-cache');

$serialisable = [ ... ];
$cacher->store('dataKey', $serialisable);

$text = "This text is stored directly without serialisation.";
$cacher->storeText('textKey', $text);

$data = $cacher->get('dataKey', 3600);
// $data = [ ... ]

$text = $cacher->getText('textKey', 3600);
// $text = "This text is stored directly without serialisation."

if ($cacher->start('cached-list.html'))
{
    $data = $this->getArticleData();
    $items = $data['items'];
    

$data = $cacher->generate('cached-data.object', function() use($someVar)
{
    $data = // calculate data...
    
    printf("Cache does not exist, generating data...");
    return $data;
});

$text = $cacher->generateText('cached-data.txt', function()
{    
    printf("Cache does not exist, generating data...");
    return 'Some text content...';
});

$html = $cacher->html('some-block.html', function()
{
    

$cacher->clear('*.html'); // clear all HTML files
$cacher->clear('*.object'); // clear all .object files
$cacher->clear(); // clear ALL cache files

// clear all cache files older than 30 days
$minimumAge = 86400 * 30;
$cacher->clear('*', true, false, $minimumAge);

class CustomFileSystem implements BlockCacher\IFileSystem
{
    function pathExists(string $path) : bool { }
    function isFile(string $path) : bool { }
    function createDirectory(string $path) : bool { }
    function readFile(string $path) : ?string { }
    function writeFile(string $path,string $contents) : bool{ }
    function deleteFile(string $path) : bool { }
    function getModifiedTime(string $path) : int { }
    function searchFiles(string $globPattern) : array { }
}

$fileSystem = new CustomFileSystem();
$cacher = new \BlockCacher\BlockCacher('cache', '', true, $fileSystem);