mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-17 23:09:10 +08:00
Added GroupDependency.
This commit is contained in:
72
framework/yii/caching/GroupDependency.php
Normal file
72
framework/yii/caching/GroupDependency.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* @link http://www.yiiframework.com/
|
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||||
|
* @license http://www.yiiframework.com/license/
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace yii\caching;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GroupDependency marks a cached data item with a group name.
|
||||||
|
*
|
||||||
|
* You may invalidate the cached data items with the same group name all at once
|
||||||
|
* by calling [[invalidate()]].
|
||||||
|
*
|
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||||
|
* @since 2.0
|
||||||
|
*/
|
||||||
|
class GroupDependency extends Dependency
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string the group name
|
||||||
|
*/
|
||||||
|
public $group;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
* @param string $group the group name
|
||||||
|
* @param array $config name-value pairs that will be used to initialize the object properties
|
||||||
|
*/
|
||||||
|
public function __construct($group, $config = array())
|
||||||
|
{
|
||||||
|
$this->group = $group;
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the data needed to determine if dependency has been changed.
|
||||||
|
* This method does nothing in this class.
|
||||||
|
* @param Cache $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)
|
||||||
|
{
|
||||||
|
if ($cache->get(array(__CLASS__, $this->group)) === false) {
|
||||||
|
// If the cutoff timestamp is not initialized or is swapped out of the cache, we need to set it.
|
||||||
|
$this->invalidate($cache, array(__CLASS__, $this->group));
|
||||||
|
}
|
||||||
|
return microtime(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the actual dependency checking.
|
||||||
|
* @param Cache $cache the cache component that is currently evaluating this dependency
|
||||||
|
* @return boolean whether the dependency is changed or not.
|
||||||
|
*/
|
||||||
|
public function getHasChanged($cache)
|
||||||
|
{
|
||||||
|
$time = $cache->get(array(__CLASS__, $this->group));
|
||||||
|
return $time === false || $time > $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidates all of the cached data items that have the same [[group]].
|
||||||
|
* @param Cache $cache the cache component that caches the data items
|
||||||
|
* @param string $group the group name
|
||||||
|
*/
|
||||||
|
public static function invalidate($cache, $group)
|
||||||
|
{
|
||||||
|
$cache->set(array(__CLASS__, $group), microtime(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user