Skip to content

pastuhov/yii2-neat-cache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Yii2 Neat cache

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

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 what two apache workers will call this function twice of 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.

About

A Yii2 components to prevent simultaneous updates (dog pile effect) during caching.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%