1234567891011121314151617181920212223242526272829303132 |
- <?php
- namespace app\components\behaviors;
- use Yii;
- use yii\base\Behavior;
- use yii\db\ActiveRecord;
- class CachedBehavior extends Behavior
- {
- public $cache_key;
- public function events()
- {
- return [
- ActiveRecord::EVENT_AFTER_INSERT => 'deleteCache',
- ActiveRecord::EVENT_AFTER_UPDATE => 'deleteCache',
- ActiveRecord::EVENT_AFTER_DELETE => 'deleteCache',
- ];
- }
- public function deleteCache()
- {
- foreach ($this->cache_key as $id){
- if( isset($this->owner->id) ){
- $key = str_replace( '##', $this->owner->id, $id );
- }else{
- $key = str_replace( '##', '', $id );
- }
- Yii::$app->cache->delete($key);
- }
- }
- }
|