mikk150 / yii2-tagdependency-invalidator
Invalidates Yii2's TagDependency tags on model update/delete and insert
Installs: 6 374
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- codeception/codeception: ^4.1.22
- codeception/module-asserts: ^1.0.0
- yiisoft/yii2-coding-standards: ^2.0
This package is auto-updated.
Last update: 2024-12-15 19:31:39 UTC
README
Usage
To use this behavior, add it to model's or components behaviors model
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', ], ] ] ] } }
then where you want to use cached models, just do this
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, ] ] ])) }
if BaseActiveRecord::EVENT_AFTER_UPDATE
, BaseActiveRecord::EVENT_AFTER_INSERT
or BaseActiveRecord::EVENT_AFTER_DELETE
is triggered, then cache is automatically invalidated for this model based on key rules
additionally, you can also clear cache by executing invalidate()
on model1