1. Go to this page and download the library: Download fuwasegu/cache-decorator 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/ */
fuwasegu / cache-decorator example snippets
use Fuwasegu\CacheDecorator;
use Some\CacheImplementation;
class ExpensiveOperation
{
#[Pure]
public function heavyComputation($param)
{
// Some expensive operation
sleep(5);
return "Result for $param";
}
}
$cacheImplementation = new CacheImplementation();
$decorator = CacheDecorator::wrap(new ExpensiveOperation(), $cacheImplementation);
// First call will be slow
$result1 = $decorator->heavyComputation('test');
// Second call will be fast, returning cached result
$result2 = $decorator->heavyComputation('test');
CacheDecorator::setDefaultTtl(3600);
$decorator = CacheDecorator::wrap($someInstance, $cacheImplementation);
// the result will be cached for 3600 seconds.
$decorator->someMethod();
class SampleController
{
public function __construct(
private Illuminate\Cache\Repository $cache,
) {}
public function __index(): Response
{
$decorator = CacheDecorator::wrap(new ExpensiveOperation(), $this->cache);
$result = $decorator->heavyComputation('test');
}
}
// Using PSR-16 SimpleCache
$psr16Cache = new SomePsr16CacheImplementation();
$decorator = CacheDecorator::wrap(new ExpensiveOperation(), $psr16Cache);
// Using PSR-6 Cache
$psr6Cache = new SomePsr6CacheImplementation();
$decorator = CacheDecorator::wrap(new ExpensiveOperation(), $psr6Cache);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.