定制 hyperia/yii2-clear-cache-behavior 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hyperia/yii2-clear-cache-behavior

最新稳定版本:v2.0.0

Composer 安装命令:

composer require hyperia/yii2-clear-cache-behavior

包简介

The behavior for Yii2 to clearing cache on specific events

README 文档

README

GitHub license Latest Stable Version

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;
              }
          ]
    ],

统计信息

  • 总下载量: 3.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-03