PHP code example of mikk150 / yii2-tagdependency-invalidator

1. Go to this page and download the library: Download mikk150/yii2-tagdependency-invalidator 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/ */

    

mikk150 / yii2-tagdependency-invalidator example snippets


class Book extends yii\base\ActiveRecord
{
    const CACHE_KEY = 'BOOKS_ARE_AWESOME!';

    public function behaviors()
    {
        return [
            [
                'class' => 'mikk150\tagdependency\InvalidateBehavior',
                'tags' => [
                    [
                        self::CACHE_KEY,
                        'id' => 'primaryKey',
                    ],
                ]
            ]
        ]
    }
}


public function actionView($id)
{
    return Yii::$app->cache->getOrSet(['book', $id], function () use ($id) {
        return Book::find()->byId($id)->one();
    }, null, new TagDependency([
        'tags' => [
            [
                Book::CACHE_KEY,
                'id' => $id,
            ]
        ]
    ]))
}