devzyj/yii2-attribute-cache-behavior
Composer 安装命令:
composer require devzyj/yii2-attribute-cache-behavior
包简介
The ActiveRecord attribute cache behavior for Yii2.
README 文档
README
提供了一些缓存 ActiveRecord 属性的方法。并且当某些事件发生时,自动使缓存失效。
Installation
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist "devzyj/yii2-attribute-cache-behavior" "~1.0.0"
or add
"devzyj/yii2-attribute-cache-behavior" : "~1.0.0"
to the require section of your application's composer.json file.
Usage
// User.php class User extends \yii\db\ActiveRecord { use \devzyj\behaviors\ActiveCacheBehaviorTrait; public function behaviors() { return [ [ 'class' => 'devzyj\behaviors\ActiveCacheBehavior', //'cache' => 'cache', //'defaultDuration' => 604800, //'baseModelCacheKey' => ['User', 'PrimaryKey'], //'keyAttributes' => static::primaryKey(), //'valueAttributes' => $this->attributes(), ], ]; } } // Using trait methods // Returns a single active record model instance. // Sets cache value if there is no cache available for the `$primaryKey`. $user = User::findOrSetOneByAttribute($primaryKey); // No changed, cache value exists. $user->save(); // Changed, cache value not exists. $user->name = 1; $user->save(); // Deleted, cache value not exists. $user->delete(); // Gets cache value for model instance. $user->getActiveCache(); // Checks cache value exists for model instance. $user->existsActiveCache(); // Sets cache value for model instance. $user->setActiveCache(); // Adds cache value for model instance. $user->addActiveCache(); // Deletes cache value for model instance. $user->deleteActiveCache(); // Using single key attribute // ActiveCacheBehavior::$keyAttributes = ['id'] $id = 1; // get cache User::instance()->getModelCacheByAttribute($id); // OR User::instance()->getModelCache(['id' => $id]); // exists cache User::instance()->existsModelCacheByAttribute($id); // OR User::instance()->existsModelCache(['id' => $id]); // set cache User::instance()->setModelCacheByAttribute($id, $value, $duration, $dependency); // OR User::instance()->setModelCache(['id' => $id], $value, $duration, $dependency); // add cache User::instance()->addModelCacheByAttribute($id, $value, $duration, $dependency); // OR User::instance()->addModelCache(['id' => $id], $value, $duration, $dependency); // delete cache User::instance()->deleteModelCacheByAttribute($id); // OR User::instance()->deleteModelCache(['id' => $id]); // get or set cache User::instance()->getOrSetModelCacheByAttribute($id, function ($behavior) use ($id) { $condition = $behavior->ensureActiveKeyAttribute($id); $model = User::findOne($condition); return $model ? $model->getActiveCacheValue() : false; }, $duration, $dependency); // OR $condition = ['id' => $id]; User::instance()->getOrSetModelCache($condition, function ($behavior) use ($condition) { $model = User::findOne($condition); return $model ? $model->getActiveCacheValue() : false; }, $duration, $dependency); // trait method: find and return ActiveRecord from cache or database User::findOrSetOneByAttribute($id, $duration, $dependency); // Using composite key attribute // ActiveCacheBehavior::$keyAttributes = ['id1', 'id2'] $id1 = 1; $id2 = 2; $ids = [$id1, $id2]; // get cache User::instance()->getModelCacheByAttribute($ids); // OR User::instance()->getModelCache(['id1' => $id1, 'id2' => $id2]); // exists cache User::instance()->existsModelCacheByAttribute($ids); // OR User::instance()->existsModelCache(['id1' => $id1, 'id2' => $id2]); // set cache User::instance()->setModelCacheByAttribute($ids, $value, $duration, $dependency); // OR User::instance()->setModelCache(['id1' => $id1, 'id2' => $id2], $value, $duration, $dependency); // add cache User::instance()->addModelCacheByAttribute($ids, $value, $duration, $dependency); // OR User::instance()->addModelCache(['id1' => $id1, 'id2' => $id2], $value, $duration, $dependency); // delete cache User::instance()->deleteModelCacheByAttribute($ids); // OR User::instance()->deleteModelCache(['id1' => $id1, 'id2' => $id2]); // get or set cache User::instance()->getOrSetModelCacheByAttribute($ids, function ($behavior) use ($ids) { $condition = $behavior->ensureActiveKeyAttribute($ids); $model = User::findOne($condition); return $model ? $model->getActiveCacheValue() : false; }, $duration, $dependency); // OR $condition = ['id1' => $id1, 'id2' => $id2]; User::instance()->getOrSetModelCache($condition, function ($behavior) use ($condition) { $model = User::findOne($condition); return $model ? $model->getActiveCacheValue() : false; }, $duration, $dependency); // trait method: find and return ActiveRecord from cache or database User::findOrSetOneByAttribute($ids, $duration, $dependency); // Using database transactions, and the `commit()` time is too long $transaction = $db->beginTransaction(); try { // old cache key. $oldKey = $model->getActiveCacheKey(); // update $model->attributes = $attributes; $model->save(); $transaction->commit(); // delete old cache. $model->deleteModelCache($oldKey); } catch (\Exception $e) { $transaction->rollBack(); throw $e; }
devzyj/yii2-attribute-cache-behavior 适用场景与选型建议
devzyj/yii2-attribute-cache-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「activerecord」 「Behavior」 「attribute」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 devzyj/yii2-attribute-cache-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 devzyj/yii2-attribute-cache-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 devzyj/yii2-attribute-cache-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CakePHP plugin to provide easy access to zumba/swivel
repository php library
ActiveRecord for API
CalendarView widget for Yii 2 Framework.
This is a set of Yii2 behaviors extensions
Relational attributes support for YII2 ActiveRecord
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-11-02