PHP code example of yii1tech / psr-cache

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/ */

    

yii1tech / psr-cache example snippets




return [
    'components' => [
        'cache' => [
            'class' => \yii1tech\psr\cache\Cache::class,
            'psrCachePool' => function () {
                // ...
                return new ExamplePsrCachePool(); // instantiate 3rd party cache library
            },
        ],
        // ...
    ],
    // ...
];



return [
    'components' => [
        'cache' => [
            'class' => \CMemCache::class,
            'servers' => [
                // ...
            ],
        ],
        \Psr\Cache\CacheItemPoolInterface::class => [
            'class' => \yii1tech\psr\cache\CacheItemPool::class,
            'cache' => 'cache',
        ],
        // ...
    ],
    // ...
];



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



return [
    'components' => [
        'cache' => [
            'class' => \yii1tech\cache\tagged\MemCache::class, // use tag aware cache component
            'servers' => [
                // ...
            ],
        ],
        \Psr\Cache\CacheItemPoolInterface::class => [
            'class' => \yii1tech\psr\cache\CacheItemPool::class,
            'cache' => '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"