pahanini/yii2-neat-cache
最新稳定版本:v0.0.4
Composer 安装命令:
composer require pahanini/yii2-neat-cache
包简介
A Yii2 components to prevent simultaneous updates (dog pile effect) during caching.
README 文档
README
#Yii2 Neat cache
About
Improved Yii2 PageCache filter to prevent dog-pile effect in yii2 applications. Please see http://www.sobstel.org/blog/preventing-dogpile-effect/ for more information about dog-pile effect.
Install
- Add
"pahanini/yii2-neat-cache": "*"to required section of your composer.json
Usage
There are two main components MutexDependency and PageCache. Both require mutex component of your application.
'components' => [ 'mutex' => [ 'class' => 'tests\components\MysqlMutex', ] ]
MutexDependency
For example you need prevent simultaneous calls of heavy function. Even if the function result is cached at the moment cache expired there is a chance that two apache workers will call this function twice or even worse.
First step to prevent this behavior is to prepare chained dependency with dependOnAll property set to false. Use first sub dependency to manage data expiration. Second dependency is MutexDependency.
$dependency = Yii::createObject([ 'class' => '\yii\caching\ChainedDependency', 'dependOnAll' => false, 'dependencies' => [ Yii::createObject([ 'class' => '\yii\caching\ExpressionDependency', 'expression' => 'Helper::isTimeToUpdate()', ]), Yii::createObject([ 'class' => '\pahanini\neatcache\MutexDependency', 'tag' => 'HeavyFunction', ]), ] ]);
If first dependency has changed for the first time then second one tries to acquire mutex lock and in case of success is considered to be changed and make cache invalid (both dependencies were changed).
Second step is to use created dependency with never expired duration value to set cache data
if (!$data = Yii::$app->cache->get('heavyDataId')) { Yii::$app->cache->set('heavyDataId', heavyFunctionCall(), 0, $dependency); }
PageCache filter
Replace native yii2 PageCache filter neat one and make cache never expired. Everlasting cache allows neat PageCache filter to use old data from expired cache to prevent dog pile effect. To make page expired you should use any of cache dependencies.
return [ 'pageCache' => [ 'class' => '\pahanini\neatcache\PageCache', 'only' => ['index'], 'duration' => 0, 'dependency' => [ 'class' => 'yii\caching\ExpressionDependency', 'expression' => '\tests\NeatCacheTest::$tag', ], ], ];
Neat PageCache automatically creates chained dependency based on specified one to prevent dog pile effect during page caching.
Testing
Copy tests config main-local.php.sample to main-local.php and run
$ phpunit
Security
If you discover any security related issues, please email pahanini@gmail.com instead of using the issue tracker.
License
The BSD License.
统计信息
- 总下载量: 5.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-11-05