PHP code example of intersvyaz / yii-tags-dependency

1. Go to this page and download the library: Download intersvyaz/yii-tags-dependency 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/ */

    

intersvyaz / yii-tags-dependency example snippets




use Intersvyaz\Cache\TagsDependency;

$cache = \Yii::app()->cache;

// save any value into cache with this dependency
$cache->set('cacheKey', 'cacheValue', 0, new TagsDependency(['A', 'B']));

// check if there is a value in cache
var_dump($cache->get('cacheKey'));

// remove (invalidate) one or several tags
TagsDependency::clearTags(['A']);

// check if cached value is absent in cache
var_dump($cache->get('cacheKey'));

class TestActiveRecord extends \CActiveRecord
{
//    ...
    
    public function behaviors()
    {
        return [
            'cacheTagBehavior' => [
                'class' => CacheTagBehavior::class,
            ]
        ];
    }
    
//    ...
}

// in other code: 

$models = TestActiveRecord::model()->cacheTag(3600, $dependency)->findAll();

// ...

// read query from cache
$models = TestActiveRecord::model()->cacheTag(3600, $dependency)->findAll();

// ...
$model2 = TestActiveRecord::model()->findByPk(2);
$model2->title = 'test';
$model2->save();

// ...

// cache invalid, read query from db
$models = TestActiveRecord::model()->cacheTag(3600, $dependency)->findAll();