CCacheDependency.php 3.3 KB

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