PHP code example of nette / caching

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'));

$value = $cache->load($key, function (Dependencies $dp) {
	$dp->expire = '20 minutes';
	return ...;
});

$value = $cache->load($key, function () {
	return ...;
}, new Dependencies(expire: '20 minutes'));

// it also accepts the number of seconds, the UNIX timestamp or DateTimeInterface
$dp->expire = '20 minutes';

$dp->sliding = true;

$dp->files[] = '/path/to/data.yaml';
// or
$dp->files = ['/path/to/data1.yaml', '/path/to/data2.yaml'];

$dp->items = ['frag1', 'frag2'];

function checkPhpVersion($ver): bool
{
	return $ver === PHP_VERSION_ID;
}

$dp->callbacks[] = ['checkPhpVersion', PHP_VERSION_ID]; // expire when checkPhpVersion(...) === false

$dp->expire = '20 minutes';
$dp->files[] = '/path/to/data.yaml';

$dp->tags = ["article/$articleId", "comments/$articleId"];

$cache->clean(tags: ["article/$articleId"]);

$cache->clean(tags: ["comments/$articleId"]);

$dp->priority = 50;

$cache->clean(priority: 100);

$cache->clean(all: true);

$values = $cache->bulkLoad($keys);

$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;
html
{cache $id, expire => '20 minutes', tags => [tag1, tag2]}
	...
{/cache}
html
{cache $id, if => !$form->isSubmitted()}
	{$form}
{/cache}