2017 © Pedro Peláez
 

yii2-extension yii2-neat-cache

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

image

pahanini/yii2-neat-cache

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

  • Friday, March 10, 2017
  • by pahanini
  • Repository
  • 1 Watchers
  • 6 Stars
  • 804 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 5 Versions
  • 4 % Grown

The README.md

Yii2 Neat cache

Build Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

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., (*2)

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., (*3)

``` php, (*4)

'components' => [ 'mutex' => [ 'class' => 'tests\components\MysqlMutex', ] ], (*5)


### 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. ```php $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)., (*6)

Second step is to use created dependency with never expired duration value to set cache data, (*7)

    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., (*8)

``` php, (*9)

return [ 'pageCache' => [ 'class' => '\pahanini\neatcache\PageCache', 'only' => ['index'], 'duration' => 0, 'dependency' => [ 'class' => 'yii\caching\ExpressionDependency', 'expression' => '\tests\NeatCacheTest::$tag', ], ], ];, (*10)


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 ``` bash $ phpunit

Security

If you discover any security related issues, please email pahanini@gmail.com instead of using the issue tracker., (*11)

License

The BSD License., (*12)

The Versions

10/03 2017

dev-master

9999999-dev https://github.com/pahanini/yii2-neat-cache

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Pavel Tetyaev

cache yii2 mutex doc pile simultaneous updates

10/03 2017

v0.0.4

0.0.4.0 https://github.com/pahanini/yii2-neat-cache

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Pavel Tetyaev

cache yii2 mutex doc pile simultaneous updates

11/01 2017

v0.0.3

0.0.3.0 https://github.com/pahanini/yii2-neat-cache

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Pavel Tetyaev

cache yii2 mutex doc pile simultaneous updates

21/01 2016

v0.0.2

0.0.2.0 https://github.com/pahanini/yii2-neat-cache

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Pavel Tetyaev

cache yii2 mutex doc pile simultaneous updates

05/11 2015

v0.0.1

0.0.1.0 https://github.com/pahanini/yii2-neat-cache

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

by Pavel Tetyaev

cache yii2 mutex doc pile simultaneous updates