PHP code example of biurad / cache

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

    

biurad / cache example snippets


use Biurad\Cache\FastCache as Cache;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;

$storage = new PhpFilesAdapter(directory: __DIR__.'/cache');
$cache = new Cache($storage);
$cache->setCacheItem(\Symfony\Component\Cache\CacheItem::class);

// The callable will only be executed on a cache miss.
$value = $cache->load(
    'my_cache_key',
    static function (CacheItemInterface $item): CacheItemInterface {
        $item->expiresAfter(3600);

        // ... do some HTTP request or heavy computations
        $item->set('foobar');

        return $item;
    }
);

echo $value; // 'foobar'

// ... and to remove the cache key
$cache->delete('my_cache_key');

// cache the result of the function
$ip = $cache->call('gethostbyaddr', "127.0.0.1");

function calculate(array $a, array $b): array {
    $result = [];
    foreach ($a as $i => $v) foreach ($v as $k => $m) $result[$i][$k] = $m + $b[$i][$k];

    return $result;
}

$matrix = $cache->wrap('calculate');
$result = $matrix([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]) // [[8, 10, 12], [14, 16, 18]]

// Caching the result of printable contents using PHP echo.
if ($block = $cache->start($key)) {
	  ... printing some data ...

	  $block->end(); // save the output to the cache
}