-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
yii\caching\CallbackDependency
to allow using a callback to d…
…etermine if a cache dependency is still valid
- Loading branch information
Showing
12 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* @link https://www.yiiframework.com/ | ||
* @copyright Copyright (c) 2008 Yii Software LLC | ||
* @license https://www.yiiframework.com/license/ | ||
*/ | ||
|
||
namespace yii\caching; | ||
|
||
/** | ||
* CallbackDependency represents a dependency based on the result of a callback function. | ||
* | ||
* Callback function should return a value that serves as the dependency data. | ||
* | ||
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). | ||
* | ||
* @author Vlad Varlamov <[email protected]> | ||
* @since 2.0.50 | ||
*/ | ||
class CallbackDependency extends Dependency | ||
{ | ||
/** | ||
* @var callable the PHP callback that will be called to determine if the dependency has been changed. | ||
*/ | ||
public $callback; | ||
|
||
|
||
/** | ||
* Generates the data needed to determine if dependency has been changed. | ||
* This method returns the result of the callback function. | ||
* @param CacheInterface $cache the cache component that is currently evaluating this dependency | ||
* @return mixed the data needed to determine if dependency has been changed. | ||
*/ | ||
protected function generateDependencyData($cache) | ||
{ | ||
return call_user_func($this->callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace yiiunit\framework\caching; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use yii\caching\ArrayCache; | ||
use yii\caching\CallbackDependency; | ||
|
||
class CallbackDependencyTest extends TestCase | ||
{ | ||
public function testDependencyChange() | ||
{ | ||
$cache = new ArrayCache(); | ||
$dependencyValue = true; | ||
|
||
$dependency = new CallbackDependency(); | ||
$dependency->callback = function () use (&$dependencyValue) { | ||
return $dependencyValue === true; | ||
}; | ||
|
||
$dependency->evaluateDependency($cache); | ||
$this->assertFalse($dependency->isChanged($cache)); | ||
|
||
$dependencyValue = false; | ||
$this->assertTrue($dependency->isChanged($cache)); | ||
} | ||
|
||
public function testDependencyNotChanged() | ||
{ | ||
$cache = new ArrayCache(); | ||
|
||
$dependency = new CallbackDependency(); | ||
$dependency->callback = function () { | ||
return 2 + 2; | ||
}; | ||
|
||
$dependency->evaluateDependency($cache); | ||
$this->assertFalse($dependency->isChanged($cache)); | ||
$this->assertFalse($dependency->isChanged($cache)); | ||
} | ||
} |