1: <?php
2: /**
3: * CCacheDependency class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CCacheDependency is the base class for cache dependency classes.
13: *
14: * CCacheDependency implements the {@link ICacheDependency} interface.
15: * Child classes should override its {@link generateDependentData} for
16: * actual dependency checking.
17: *
18: * @property boolean $hasChanged Whether the dependency has changed.
19: * @property mixed $dependentData The data used to determine if dependency has been changed.
20: * This data is available after {@link evaluateDependency} is called.
21: *
22: * @author Qiang Xue <qiang.xue@gmail.com>
23: * @package system.caching.dependencies
24: * @since 1.0
25: */
26: class CCacheDependency extends CComponent implements ICacheDependency
27: {
28: /**
29: * @var boolean Whether this dependency is reusable or not.
30: * If set to true, dependent data for this cache dependency will only be generated once per request.
31: * You can then use the same cache dependency for multiple separate cache calls on the same page
32: * without the overhead of re-evaluating the dependency each time.
33: * Defaults to false;
34: * @since 1.1.11
35: */
36: public $reuseDependentData=false;
37:
38: /**
39: * @var array cached data for reusable dependencies.
40: * @since 1.1.11
41: */
42: private static $_reusableData=array();
43:
44: private $_hash;
45: private $_data;
46:
47: /**
48: * Evaluates the dependency by generating and saving the data related with dependency.
49: * This method is invoked by cache before writing data into it.
50: */
51: public function evaluateDependency()
52: {
53: if ($this->reuseDependentData)
54: {
55: $hash=$this->getHash();
56: if(!isset(self::$_reusableData[$hash]['dependentData']))
57: self::$_reusableData[$hash]['dependentData']=$this->generateDependentData();
58: $this->_data=self::$_reusableData[$hash]['dependentData'];
59: }
60: else
61: $this->_data=$this->generateDependentData();
62: }
63:
64: /**
65: * @return boolean whether the dependency has changed.
66: */
67: public function getHasChanged()
68: {
69: if ($this->reuseDependentData)
70: {
71: $hash=$this->getHash();
72: if(!isset(self::$_reusableData[$hash]['dependentData']))
73: self::$_reusableData[$hash]['dependentData']=$this->generateDependentData();
74: return self::$_reusableData[$hash]['dependentData']!=$this->_data;
75: }
76: else
77: return $this->generateDependentData()!=$this->_data;
78: }
79:
80: /**
81: * @return mixed the data used to determine if dependency has been changed.
82: * This data is available after {@link evaluateDependency} is called.
83: */
84: public function getDependentData()
85: {
86: return $this->_data;
87: }
88:
89: /**
90: * Resets cached data for reusable dependencies.
91: * @since 1.1.14
92: */
93: public static function resetReusableData()
94: {
95: self::$_reusableData=array();
96: }
97:
98: /**
99: * Generates the data needed to determine if dependency has been changed.
100: * Derived classes should override this method to generate actual dependent data.
101: * @return mixed the data needed to determine if dependency has been changed.
102: */
103: protected function generateDependentData()
104: {
105: return null;
106: }
107: /**
108: * Generates a unique hash that identifies this cache dependency.
109: * @return string the hash for this cache dependency
110: */
111: private function getHash()
112: {
113: if($this->_hash===null)
114: $this->_hash=sha1(serialize($this));
115: return $this->_hash;
116: }
117: }