hyperia / yii2-clear-cache-behavior
The behavior for Yii2 to clearing cache on specific events
Installs: 3 194
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 1
Open Issues: 1
Type:yii2-extension
Requires
- php: >=7.1
- yiisoft/yii2: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-28 20:01:35 UTC
README
The behavior for Yii2 to clearing cache on specific events
Instalation
The preferred way to install this extension is through composer.
Either run
composer require hyperia/yii2-clear-cache-behavior:"*"
or add
"hyperia/yii2-clear-cache-behavior": "*"
to the require section of your composer.json.
Configuration (usage)
In ActiveRecord Model class to invalidate tag after insert, update or delete
use yii\db\ActiveRecord; use hyperia\behaviors\ClearCacheBehavior; class Model extends ActiveRecord { private const CACHE_KEY = '~model~'; public function behaviors() { return [ ... 'clearCache' => [ 'class' => ClearCacheBehavior::class, 'events' => [ ActiveRecord::EVENT_AFTER_INSERT, ActiveRecord::EVENT_AFTER_UPDATE, ActiveRecord::EVENT_AFTER_DELETE ], 'type' => ClearCacheBehavior::TYPE_INVALIDATE_TAG, 'value' => static::CACHE_KEY ], ]; } }
Parameter description
events
array
Determinantes on which event would be cache deleted. When you want set up Event with same settings.
Default value:
[
ActiveRecord::EVENT_AFTER_INSERT,
ActiveRecord::EVENT_AFTER_UPDATE,
ActiveRecord::EVENT_AFTER_DELETE
]
cache
string
Name of cache component in yii components configuration
Default: "cache"
value
string | array | Closure
Determinantes which part of cache would be deleted ONLY WHEN EVENTS IS SET
type
string
Sets how the cache will be deleted ONLY WHEN EVENTS IS SET
Types:
- TYPE_INVALIDATE_TAG : Calls yii\caching\TagDependency::invalidate($cacheObject, $value);
- TYPE_FLUSH : Calls flush() method on cache object (by value of cache parameter)
- TYPE_DELETE : Calls delete($value) method on cache object (by value of cache parameter)
events_with_settings
array
Array which represents setting of multiple events. Determinantes on which event would be cache deleted. When you want set up multiple Events
Simple example:
'eventsWithSettings' => [ \yii\web\Controller::EVENT_BEFORE_ACTION => [ 'type' => ClearCacheBehavior::TYPE_INVALIDATE_TAG, 'value' => static::CACHE_KEY ], \yii\web\Controller::EVENT_AFTER_ACTION => [ 'type' => ClearCacheBehavior::TYPE_DELETE, 'value' => function($event) use ($model) { return $model->id; } ] ],