Dependency.php 3.14 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8 9
namespace yii\caching;

Qiang Xue committed
10
/**
Qiang Xue committed
11
 * Dependency is the base class for cache dependency classes.
Qiang Xue committed
12
 *
Qiang Xue committed
13 14
 * Child classes should override its [[generateDependencyData()]] for generating
 * the actual dependency data.
Qiang Xue committed
15 16 17 18
 *
 * @property boolean $hasChanged Whether the dependency has changed.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
19
 * @since 2.0
Qiang Xue committed
20
 */
Qiang Xue committed
21
abstract class Dependency extends \yii\base\Object
Qiang Xue committed
22 23
{
	/**
Qiang Xue committed
24 25
	 * @var mixed the dependency data that is saved in cache and later is compared with the
	 * latest dependency data.
Qiang Xue committed
26
	 */
Qiang Xue committed
27
	public $data;
28 29
	/**
	 * @var boolean whether this dependency is reusable or not. True value means that dependent
30
	 * data for this cache dependency will be generated only once per request. This allows you
31 32 33
	 * to use the same cache dependency for multiple separate cache calls while generating the same
	 * page without an overhead of re-evaluating dependency data each time. Defaults to false.
	 */
34
	public $reusable = false;
35 36 37 38 39 40 41 42 43

	/**
	 * @var array static storage of cached data for reusable dependencies.
	 */
	private static $_reusableData = array();
	/**
	 * @var string a unique hash value for this cache dependency.
	 */
	private $_hash;
Qiang Xue committed
44

45

Qiang Xue committed
46 47 48
	/**
	 * Evaluates the dependency by generating and saving the data related with dependency.
	 * This method is invoked by cache before writing data into it.
49
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
50
	 */
51
	public function evaluateDependency($cache)
Qiang Xue committed
52
	{
53
		if (!$this->reusable) {
54
			$this->data = $this->generateDependencyData($cache);
55 56 57 58 59
		} else {
			if ($this->_hash === null) {
				$this->_hash = sha1(serialize($this));
			}
			if (!array_key_exists($this->_hash, self::$_reusableData)) {
60
				self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache);
61 62 63
			}
			$this->data = self::$_reusableData[$this->_hash];
		}
Qiang Xue committed
64 65 66
	}

	/**
67 68
	 * Returns a value indicating whether the dependency has changed.
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
69 70
	 * @return boolean whether the dependency has changed.
	 */
71
	public function getHasChanged($cache)
Qiang Xue committed
72
	{
73
		if (!$this->reusable) {
74
			return $this->generateDependencyData($cache) !== $this->data;
75 76 77 78 79
		} else {
			if ($this->_hash === null) {
				$this->_hash = sha1(serialize($this));
			}
			if (!array_key_exists($this->_hash, self::$_reusableData)) {
80
				self::$_reusableData[$this->_hash] = $this->generateDependencyData($cache);
81
			}
Qiang Xue committed
82
			return self::$_reusableData[$this->_hash] !== $this->data;
83 84 85 86 87 88 89 90 91
		}
	}

	/**
	 * Resets all cached data for reusable dependencies.
	 */
	public static function resetReusableData()
	{
		self::$_reusableData = array();
Qiang Xue committed
92 93 94 95
	}

	/**
	 * Generates the data needed to determine if dependency has been changed.
Qiang Xue committed
96
	 * Derived classes should override this method to generate the actual dependency data.
97
	 * @param Cache $cache the cache component that is currently evaluating this dependency
Qiang Xue committed
98 99
	 * @return mixed the data needed to determine if dependency has been changed.
	 */
100
	abstract protected function generateDependencyData($cache);
Zander Baldwin committed
101
}