pahanini / yii2-neat-cache
A Yii2 components to prevent simultaneous updates (dog pile effect) during caching.
Installs: 4 733
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 3
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- ext-curl: *
- fxp/composer-asset-plugin: *
- yiisoft/yii2: 2.0.*
Requires (Dev)
- phpunit/phpunit: 4.8.*
This package is auto-updated.
Last update: 2024-12-18 18:00:01 UTC
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.