1. Go to this page and download the library: Download yii1tech/psr-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/ */
use Psr\Cache\CacheItemPoolInterface;
function getCachedValue()
{
/** @var CacheItemPoolInterface $pool */
$pool = Yii::app()->getComponent(CacheItemPoolInterface::class);
$item = $pool->getItem('example-cache-id');
if ($item->isHit()) {
return $item->get(); // cache exist - return cached value
}
$value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query.
$item->set($value) // set value to be cached
->expiresAfter(DateInterval::createFromDateString('1 hour')); // set expiration
$pool->save($item); // put value into cache
return $value;
}
use yii1tech\psr\cache\CacheItemPoolContract;
function getValueCachedWithDependency()
{
/** @var CacheItemPoolContract $pool */
$pool = Yii::app()->getComponent(CacheItemPoolContract::class);
$item = $pool->getItem('example-cache-id');
if ($item->isHit()) {
return $item->get(); // cache exist - return cached value
}
$value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query.
$item->set($value) // set value to be cached
->expiresAfter(DateInterval::createFromDateString('1 hour')) // set expiration
->depends(new CDbCacheDependency('SELECT MAX(id) FROM `items`')); // set cache dependency
$pool->save($item); // put value into cache
return $value;
}
use yii1tech\psr\cache\CacheItemContract;
use yii1tech\psr\cache\CacheItemPoolContract;
function getCachedValue()
{
/** @var CacheItemPoolContract $pool */
$pool = Yii::app()->getComponent(CacheItemPoolContract::class);
return $pool->get('example-cache-id', function (CacheItemContract $item) {
// enters here, only if cache is missing
$item->expiresAfter(DateInterval::createFromDateString('1 hour')); // use callback argument to configure cache item: set expiration and so on
$value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query.
return $value; // returned value automatically saved ot cache
});
}
use yii1tech\psr\cache\CacheItemContract;
use yii1tech\psr\cache\CacheItemPoolContract;
function getCachedValue()
{
/** @var CacheItemPoolContract $pool */
$pool = Yii::app()->getComponent(CacheItemPoolContract::class);
return $pool->get('example-cache-id', function (CacheItemContract $item) {
$item->expiresAfter(DateInterval::createFromDateString('1 hour'));
$item->tag(['database', 'example']); // specify the list of tags for the item
$value = Yii::app()->db->createCommand('SELECT ...')->query(); // some heave SQL query.
return $value;
});
}
use yii1tech\psr\cache\CacheItemPoolContract;
/** @var CacheItemPoolInterface $pool */
$pool = Yii::app()->getComponent(CacheItemPoolInterface::class);
$pool->invalidateTags(['database']); // clear only items tagged as "database"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.