1. Go to this page and download the library: Download nette/caching 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/ */
nette / caching example snippets
use Nette\Caching\Cache;
use Nette\Caching\Dependencies;
$storage // instance of Nette\Caching\Storage
$cache = new Cache($storage, 'Full Html Pages');
$value = $cache->load($key, function () use ($key) {
$computedValue = ...; // heavy computations
return $computedValue;
});
$cache->remove($key);
$result = $cache->call('gethostbyaddr', $ip);
function factorial($num)
{
return ...;
}
$memoizedFactorial = $cache->wrap('factorial');
$result = $memoizedFactorial(5); // counts it
$result = $memoizedFactorial(5); // returns it from cache
$cache->save($key, $value, new Dependencies(expire: '20 minutes'));
$values = $cache->bulkLoad($keys, function ($key, Dependencies $dp) {
$computedValue = ...; // heavy computations
return $computedValue;
});
if ($capture = $cache->capture($key)) {
echo ... // printing some data
$capture->end(); // save the output to the cache
}
$psrCache = new Nette\Bridges\Psr\PsrCacheAdapter($storage);
$psrCache->set('key', 'value', 3600); // stores the value for 1 hour
$value = $psrCache->get('key', 'default');
// the storage will be the directory '/path/to/temp' on the disk
$storage = new Nette\Caching\Storages\FileStorage('/path/to/temp');
$storage = new Nette\Caching\Storages\MemcachedStorage('10.0.0.158');
$storage = new Nette\Caching\Storages\RedisStorage('10.0.0.158');
$storage = new Nette\Caching\Storages\RedisStorage('10.0.0.158', lockTimeout: 120);
$storage = new Nette\Caching\Storages\MemoryStorage;
$storage = new Nette\Caching\Storages\SQLiteStorage('/path/to/cache.sdb');
// $simpleCache is any Psr\SimpleCache\CacheInterface
$storage = new Nette\Bridges\Psr\Psr16Storage($simpleCache, journal: $journal);
$storage = new Nette\Caching\Storages\DevNullStorage;