定制 consik/yii2-cachecleaner 二次开发

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

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

consik/yii2-cachecleaner

Composer 安装命令:

composer require consik/yii2-cachecleaner

包简介

Yii2 behavior for deleting cache values on component event

README 文档

README

Latest Stable Version Total Downloads License

Installation

The preferred way to install this extension is through composer.

Either run

composer require consik/yii2-cachecleaner

or add

"consik/yii2-cachecleaner": "^1.0"

CacheCleanerBehavior class description

Deletes cache after defined events.

Properties

  1. array $events = [] - Associative array where key is event name, value(array|string|null) is a cache key(s) to delete
  2. string $cacheComponent = 'cache' - Name of the application cache component

Public methods

  1. boolean deleteCache(string|array $key) - Deletes cache value(s)
  2. boolean flushCache() - Deletes all cache values

ARUpdateCCBehavior class description. #docs

Deletes cache values after updating ActiveRecord attributes

Properties

  1. array $attributes = [] - Associative array, where array keys is attributes name, array value is cache value(s) keys
  2. string $cacheComponent = 'cache' - Name of the application cache component

See DocBlock for usage examples.

Examples

Autodelete cache after AR update

Simple use case: We have cached AR object somewhere in our app:

<?php
...
if (!$model = Yii::$app->cache->get('cachedModel')) {
	$model = ARModel::findOne($modelID);
	Yii::$app->cache->set('cachedModel', $model);
}
...

So, if somewhere in applicaton we change or delete this AR data, we have to delete our cache value.

Just use CacheCleanerBehavior in your ARModel:

<?php
public function behaviors()
{
    return [[
        'class' => CacheCleanerBehavior::className(),
        //'cacheComponent' => 'cache',  //you can define your app cache component
        'events' => [
            ActiveRecord::EVENT_AFTER_UPDATE => 'cachedModel'
            ActiveRecord::EVENT_BEFORE_DELETE => 'cachedModel'
        ]
    ]];
}

NOTE! When component initialize behaviors there is no attributes! To set cache keys with object attributes use callable param for defining. #DocBlock

Using CacheUpdateEvent

There is a special class in package for triggering events when you change cache values, where you can send to handler action changed cache key(s).

You can use this class with CacheCleanerBehavior if you want delete different keys using one event name.

Example:

<?php
public function behaviors()
{
    return [[
        'class' => CacheCleanerBehavior::className(),
        'events' => [
            YOUR_EVENT_NAME => null
        ]
    ]];
}

...

function someComponentAction()
{
...
	$this->trigger(YOUR_EVENT_NAME, new CacheUpdateEvent([
		'keys' => ['keyName1', 'keyName2'],
	]));
...
}

Other options for defining $events property

See the doc block for CacheCleanerBehavior::$events

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固