intersvyaz/yii2-tag-dependency-helper
最新稳定版本:1.0.1
Composer 安装命令:
composer require intersvyaz/yii2-tag-dependency-helper
包简介
Helper for unifying cache tag names with invalidation support in yii2
README 文档
README
Helper for unifying cache tag names with invalidation support for Yii2.
Usage
In your model add behavior:
... use Intersvyaz\TagDependencyHelper\Traits\CachedFind; use Intersvyaz\TagDependencyHelper\InvalidateTagBehavior; ... class Configurable extends ActiveRecord { use CachedFind; /** * @inheritdoc */ public function behaviors() { return [ [ 'class' => InvalidateTagBehavior::class, 'cache' => 'cache', // optional option - application id of cache component ], ]; } }
This behavior automatically invalidates tags by model name and pair model-id.
If your cache entry should be flushed every time any row of model is edited - use getCommonTag helper function:
use Intersvyaz\TagDependencyHelper\ActiveRecordCacheTags; ... $models = Configurable::getDb()->cache( function ($db) { return Configurable::find()->all($db); }, 86400, new TagDependency([ 'tags' => [ActiveRecordCacheTags::getCommonTag(Configurable::className())], ]) );
If your cache entry should be flushed only when exact row of model is edited - use getObjectTag helper function:
use Intersvyaz\TagDependencyHelper\ActiveRecordCacheTags; ... $cacheKey = 'Product:' . $model_id; if (false === $product = Yii::$app->cache->get($cacheKey)) { if (null === $product = Product::findOne($model_id)) { throw new NotFoundHttpException; } Yii::$app->cache->set( $cacheKey, $product, 86400, new TagDependency( [ 'tags' => [ ActiveRecordCacheTags::getObjectTag(Product::class, $model_id), ] ] ) ); }
In the CachedFind trait implemented shortcut method cachedFindOne() and cachedFindAll() for cached find query.
统计信息
- 总下载量: 105
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-05